How to update iframe automatically

I would like the iframe to be updated to the specified source path. What is the easiest way to do this with javascript?

thanks,

+9
javascript html iframe
source share
6 answers

If you are probably looking for a way to do this without javascript enabled and have access to change the iframe HTML, then you can add this reload code to the <head> iframe HTML tag:

 <meta http-equiv="refresh" content="600"> 
+12
source share

Something like that:

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

The interval resets the iframe src attribute every 2 seconds (2000 milliseconds)

Edit to respond to your comments: This, like any other code that manipulates the DOM, must be in the window.onload event listener or the like:

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

try it

 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 it takes a while to load the page, that is, more than 3 seconds, you will not see anything.

Hope this helps.

+1
source share

try it

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


Interval time is 3 seconds.

+1
source share

If you want to update the iframe itself.

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

and you can try it.

 window.frameElement.src = window.frameElement.src 
0
source share

All Articles