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


当前回答

如果你使用Jquery,那么有一行代码。

$('#iframeID',window.parent.document).attr('src',$('#iframeID',window.parent.document).attr('src'));

如果你们是和同一个家长一起工作的话

$('#iframeID',parent.document).attr('src',$('#iframeID',parent.document).attr('src'));

其他回答

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;
}

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

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

从Iframe内部重新加载

如果你的应用程序在Iframe中,你可以通过替换位置href来刷新它:

document.location.href = document.location.href

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

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