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


当前回答

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

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

其他回答

另一个解决方案。

const frame = document.getElementById("my-iframe");

frame.parentNode.replaceChild(frame.cloneNode(), frame);

从Iframe内部重新加载

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

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

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

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

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