我想重新加载一个<iframe>使用JavaScript。到目前为止,我发现最好的方法是将iframe的src属性设置为自身,但这不是很干净。什么好主意吗?


当前回答

出于调试目的,可以打开控制台,将执行上下文更改为他想要刷新的帧,然后执行document.location.reload()

其他回答

document.getElementById('iframeid').src = document.getElementById('iframeid').src

它将重新加载iframe,甚至跨域! 通过IE7/8, Firefox和Chrome测试。

注意:正如@user85461所提到的,如果iframe src URL中有一个散列,这种方法就不起作用了(例如http://example.com/#something)。

<script type="text/javascript">
  top.frames['DetailFrame'].location = top.frames['DetailFrame'].location;
</script> 

在IE8中使用。net,设置iframe。SRC第一次是可以的, 而是设置iframe。SRC第二次没有引发iframed页面的page_load。 为了解决这个问题,我使用了iframe.contentDocument.location.href = "NewUrl.htm"。

当使用jQuery thickBox并尝试在thickBox iframe中重新打开同一页面时发现它。 然后它只显示之前打开的页面。

这种方法避免了向某些浏览器添加历史记录(不必要的开销)。正文部分:

    <div id='IF'>
    <iframe src='https://www.wolframalpha.com/input?i=Memphis%20TN%20Temperature' 
        style="width:5in; height:6in"  // or whatever you want in your Iframe
        title'Temperature'></iframe>
    </div>

然后在某些JAVASCRIPT中,你可能会有这样的函数:

    function    UPdate()    { // Iframe
    T1=document.getElementById('IF')
    T2=T1.innerHTML
    T1.innerHTML=T2
}
document.getElementById('some_frame_id').contentWindow.location.reload();

注意,在Firefox中,window.frames[]不能通过id索引,而是通过名称或索引