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


当前回答

使用self.location.reload()将重载iframe。

<iframe src=“https://vivekkukumar11432.wordpress.com/“width="300" " height="300"></iframe> < br > < br > <输入类型=“按钮”值=“重新加载”/>

其他回答

IE使用reload,其他浏览器使用set src。(重新装填对FF无效) 在IE 7、8、9和Firefox上测试

if(navigator.appName == "Microsoft Internet Explorer"){
    window.document.getElementById('iframeId').contentWindow.location.reload(true);
}else {
    window.document.getElementById('iframeId').src = window.document.getElementById('iframeId').src;
}

新url

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

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

重新加载

location.reload();

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

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

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

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

window.frames['frameNameOrIndex'].location.reload();

如果以上方法对你都不起作用:

window.location.reload();

出于某种原因,这刷新了我的iframe,而不是整个脚本。也许是因为它被放置在帧本身,而所有这些getElemntById解决方案工作时,你试图刷新一个帧从另一个帧?

或者我不完全理解这一点,说话胡言乱语,无论如何,这对我来说就像一个魅力:)