how to refresh an iframe automatically

2019-01-18 12:59发布

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

thanks,

6条回答
贪生不怕死
2楼-- · 2019-01-18 13:16

If you want to refresh an iframe itself.

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

and you can try this.

window.frameElement.src = window.frameElement.src
查看更多
对你真心纯属浪费
3楼-- · 2019-01-18 13:19

Something like this:

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

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

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

<script type=text/javascript>
window.onload = function() {
    var frameRefreshInterval = setInterval(2000, function() {
        document.getElementById("myframe").src = document.getElementById("myframe").src
    });
    // any other code
}
</script>
查看更多
淡お忘
4楼-- · 2019-01-18 13:20

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:

<meta http-equiv="refresh" content="600">
查看更多
爷的心禁止访问
5楼-- · 2019-01-18 13:25
setInterval(function(){
    document.getElementById("myframe").src+="";
},2500);
查看更多
神经病院院长
6楼-- · 2019-01-18 13:25

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.

Hope this helps.

查看更多
We Are One
7楼-- · 2019-01-18 13:26

Try This

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

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


Interval Time Is 3 Second.

查看更多
登录 后发表回答