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


当前回答

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

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

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

其他回答

您是否考虑过向url附加一个毫无意义的查询字符串参数?

<iframe src="myBaseURL.com/something/" />

<script>
var i = document.getElementsById("iframe")[0],
    src = i.src,
    number = 1;

//For an update
i.src = src + "?ignoreMe=" + number;
number++;
</script>

它不会被看到&如果你意识到参数是安全的,那么它应该是好的。

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

现在让这个工作在chrome 66,试试这个:

const reloadIframe = (iframeId) => {
    const el = document.getElementById(iframeId)
    const src = el.src
    el.src = ''
    setTimeout(() => {
        el.src = src
    })
}

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

document.getElementById('some_frame_id').contentWindow.location.reload();

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