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


当前回答

如果使用jQuery,这似乎是可行的:

$('#your_iframe').attr('src', $('#your_iframe').attr('src'));

其他回答

window.frames['frameNameOrIndex'].location.reload();
document.getElementById('some_frame_id').contentWindow.location.reload();

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

我刚刚遇到这在铬和唯一的工作是删除和替换iframe。例子:

$(".iframe_wrapper").find("iframe").remove();
var iframe = $('<iframe src="' + src + '" frameborder="0"></iframe>');
$.find(".iframe_wrapper").append(iframe);

很简单,其他答案中没有提到。

新url

location.assign("http:google.com");

assign()方法加载一个新文档。

重新加载

location.reload();

reload()方法用于重新加载当前文档。

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

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