我想重新加载一个<iframe>使用JavaScript。到目前为止,我发现最好的方法是将iframe的src属性设置为自身,但这不是很干净。什么好主意吗?
当前回答
document.getElementById('iframeid').src = document.getElementById('iframeid').src
它将重新加载iframe,甚至跨域! 通过IE7/8, Firefox和Chrome测试。
注意:正如@user85461所提到的,如果iframe src URL中有一个散列,这种方法就不起作用了(例如http://example.com/#something)。
其他回答
如果你使用Jquery,那么有一行代码。
$('#iframeID',window.parent.document).attr('src',$('#iframeID',window.parent.document).attr('src'));
如果你们是和同一个家长一起工作的话
$('#iframeID',parent.document).attr('src',$('#iframeID',parent.document).attr('src'));
window.frames['frameNameOrIndex'].location.reload();
<script type="text/javascript">
top.frames['DetailFrame'].location = top.frames['DetailFrame'].location;
</script>
document.getElementById('some_frame_id').contentWindow.location.reload();
注意,在Firefox中,window.frames[]不能通过id索引,而是通过名称或索引
如果使用jQuery,这似乎是可行的:
$('#your_iframe').attr('src', $('#your_iframe').attr('src'));