我想重新加载一个<iframe>使用JavaScript。到目前为止,我发现最好的方法是将iframe的src属性设置为自身,但这不是很干净。什么好主意吗?
当前回答
如果以上方法对你都不起作用:
window.location.reload();
出于某种原因,这刷新了我的iframe,而不是整个脚本。也许是因为它被放置在帧本身,而所有这些getElemntById解决方案工作时,你试图刷新一个帧从另一个帧?
或者我不完全理解这一点,说话胡言乱语,无论如何,这对我来说就像一个魅力:)
其他回答
如果使用jQuery,这似乎是可行的:
$('#your_iframe').attr('src', $('#your_iframe').attr('src'));
window.frames['frameNameOrIndex'].location.reload();
document.getElementById('some_frame_id').contentWindow.location.reload();
注意,在Firefox中,window.frames[]不能通过id索引,而是通过名称或索引
现在让这个工作在chrome 66,试试这个:
const reloadIframe = (iframeId) => {
const el = document.getElementById(iframeId)
const src = el.src
el.src = ''
setTimeout(() => {
el.src = src
})
}
这种方法避免了向某些浏览器添加历史记录(不必要的开销)。正文部分:
<div id='IF'>
<iframe src='https://www.wolframalpha.com/input?i=Memphis%20TN%20Temperature'
style="width:5in; height:6in" // or whatever you want in your Iframe
title'Temperature'></iframe>
</div>
然后在某些JAVASCRIPT中,你可能会有这样的函数:
function UPdate() { // Iframe
T1=document.getElementById('IF')
T2=T1.innerHTML
T1.innerHTML=T2
}