how to refresh an iframe automatically

2019-01-18 13:33发布

问题:

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,

回答1:

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">


回答2:

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>


回答3:

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


回答4:

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.



回答5:

Try This

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

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


Interval Time Is 3 Second.



回答6:

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