Javascript 如何自动刷新 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6975765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:04:41  来源:igfitidea点击:

how to refresh an iframe automatically

javascripthtmliframe

提问by triq

I would like an iframe to refresh itself to the source path specified. What is the easiest way to do this inline with javascript?

我想要一个 iframe 将自身刷新到指定的源路径。使用 javascript 内联执行此操作的最简单方法是什么?

thanks,

谢谢,

回答by Curt

If you're also perhaps looking for a way of doing this without javascript enabled, and have access to modify the iframe HTML, then you could add this reload code to the <head>tag of the iframe HTML:

如果您可能也在寻找一种在不启用 javascript 的情况下执行此操作的方法,并且有权修改 iframe HTML,那么您可以将此重新加载代码添加到<head>iframe HTML的标记中:

<meta http-equiv="refresh" content="600">

回答by Griffin

You can use setInterval(function, milliseconds, param1, param2, ...)

您可以使用 setInterval(function, milliseconds, param1, param2, ...)

Something like this:

像这样的东西:

var frameRefreshInterval = setInterval(function() {
    document.getElementById("myframe").src = document.getElementById("myframe").src
}, 2000);

The interval resets the iframe's srcattribute ever 2 seconds (2000 milliseconds)

间隔每src2 秒(2000 毫秒)重置一次iframe 的属性

Edit, to answer you comments:This, along with any other code that manipulates the DOM, should be in a window.onloadevent listener or similar like so:

编辑,回答您的评论:这与操作 DOM 的任何其他代码一起,应该在window.onload事件侦听器或类似的东西中:

<script type=text/javascript>
window.onload = function() {
    var frameRefreshInterval = setInterval(function() {
        document.getElementById("myframe").src = document.getElementById("myframe").src
    }, 2000);
    // any other code
}
</script>

回答by gion_13

setInterval(function(){
    document.getElementById("myframe").src+="";
},2500);

回答by Ash Burlaczenko

Try this

尝试这个

function refresh()
{
    var iframe = document.getElementById('iframe');
    iframe.reload(true);
}

setTimeout('refresh()', 3000);

Note, this will try to refresh the page every 3 seconds. Obviously, if the page takes a while to load, i.e. over 3 seconds then you wont see anything.

请注意,这将尝试每 3 秒刷新一次页面。显然,如果页面加载需要一段时间,即超过 3 秒,那么您将看不到任何内容。

Hope this helps.

希望这可以帮助。

回答by JaydipLila-Jd3005

Try This

尝试这个

<script>
     window.setInterval("reloadIFrame();", 3000);

     function reloadIFrame()
     {
          document.frames["frameNameHere"].location.reload();
     }
</script>


Interval Time Is 3 Second.


间隔时间为 3 秒。

回答by alan9uo

If you want to refresh an iframe itself.

如果要刷新 iframe 本身。

document.getElementById('iframe');//this may not work

and you can try this.

你可以试试这个。

window.frameElement.src = window.frameElement.src