0

I want an iframe to refresh every 5 seconds, but it doesn't work and I don't understand why? I manually changed the text file but it won't refresh on the site automatically. Is it any special with text files?

<!DOCTYPE html>
<html>
 <head>
  <title>Example</title>
 </head>
 <body>
  <script>
  window.setInterval("reloadIFrame();", 5000);
  function reloadIFrame() {
   document.frames["knasFrame"].location.reload();
  }
  </script>
  <iframe name="knasFrame" src="output.txt"></iframe>
 </body>
</html>
dferrar
  • 17
  • 1
  • 6
Knas
  • 1
  • 1
    possible duplicate: http://stackoverflow.com/questions/5146805/an-iframe-i-need-to-refresh-every-30-seconds-but-not-the-whole-page. – Rohit416 Sep 03 '15 at 19:14

3 Answers3

1

<!DOCTYPE html>
<html>
 <head>
  <title>Example</title>
 </head>
 <body>
  <script>
  window.setInterval("reloadIFrame();", 5000);
  function reloadIFrame() {
                     document.getElementsByName("knasFrame")[0].contentWindow.location.reload();
  }
  </script>
  <iframe name="knasFrame" src="output.txt"></iframe>
 </body>
</html>
Sagi
  • 8,972
  • 3
  • 33
  • 41
  • It does not work either. if i debug it stops with that code. – Knas Sep 03 '15 at 19:44
  • both with your and my code but if i use "document.getElementById("knasFrame").src = document.getElementById("knasFrame").src" – Knas Sep 03 '15 at 19:48
  • It works maybe your path to the src is not well defined. – Sagi Sep 03 '15 at 19:49
  • ...then it works but it flashes every time it updates . Is it possible to fix in any other way ? – Knas Sep 03 '15 at 19:50
  • I've edited the code to find by name and not by id because your element does not have an id – Sagi Sep 03 '15 at 19:51
  • Of course it flashes you reload the page. Please describe what do you really need for the page to do. – Sagi Sep 03 '15 at 19:53
0

You can debug your problem by replacing the whole function by an alert() just to see if the interval is working.

Change also <script> to <script type="text/javascript">

Dan Rey Oquindo
  • 276
  • 1
  • 3
  • 11
0

This will work - (attention) for demo purposes I've changed you time interval to a timeout so that it will happen only once. and to make the reload visually apparent I'm adding a in-page navigation hash to your question.

<!DOCTYPE html>
<html>
 <head>
  <title>Example</title>
 </head>
 <body>
  <iframe name="knasFrame" 
                src="http://stackoverflow.com/questions/32383555/javascript-wont-autorefresh-iframe/" 
                height=300px 
                width=100%>
      </iframe>
      
      <script>

SCRIPT DELETED BY AUTHOR
for general security reasons
      
      </script>

 </body>
</html>
Bekim Bacaj
  • 5,707
  • 2
  • 24
  • 26