2015-12-06 52 views
2

如何自動刷新一個iframe每3秒刷新整個頁面。我使用<meta http-equiv="refresh" content="1">,但它會顯示整個頁面刷新,並在每次刷新頁面時將其顯示在頁面頂部。我的iframe指向一個文本文件來讀取我放入的實時消息。是否有辦法做到這一點,刷新整個頁面,只是元素。我是一個HTML初學者,請詳細解釋。我谷歌很多,並嘗試了很多東西,迄今沒有任何作品,我嘗試在許多瀏覽器。自動刷新IFrame HTML

到目前爲止我的代碼:

<!DOCTYPE html> 
<html> 
<head> 
<link rel="shortcut icon" href="favicon.png" type="image/x-icon" /> 



<title>Chat</title> 

<style> 

body{ 
margin: 0px; 
} 

h1{ 
font-family: arial; 
font-size: 100%; 
} 
iframe{ 
width: 900px; 
height: 500px; 
} 

div.top{ 
background-color: rgba(2, 109, 8, 0.83); 
height: 30px; 
width: 100%; 
} 
</style> 


</head> 

<body> 
<div class="top"> 
<img src="favicon.png" alt="favicon" height="31" width="31" > 
<h1>Chat</h1> 
</div> 




<iframe src="text.txt" name="iframe_a"> /> 



</body> 
</html> 
+0

你應該看看[這個問題](http://stackoverflow.com/questions/25446628/ajax-jquery-refresh-div-every-5-seconds)。 – Berendschot

+0

http://stackoverflow.com/q/86428/2517622 – drets

+0

仍然沒有幫助。同樣的事情 –

回答

1

你可以完成你需要通過簡單地使用Javascript的幾句什麼。

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

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

不適用於Firefox或Chrome或IE。 –

+0

我把它放進去,什麼也沒有發生。 –

+1

你用框架名稱替換它嗎? – Hakam

0

您的html標籤有問題。 (以下簡稱「IFRAME」標籤格式不正確)

在這裏,你有你的代碼固定的,你的答案就可以運行:

<!DOCTYPE html> 
<html> 
<head> 
    <link rel="shortcut icon" href="favicon.png" type="image/x-icon" /> 
    <title>Chat</title> 
    <style> 
     body { 
      margin: 0px; 
     } 
     h1 { 
      font-family: arial; 
      font-size: 100%; 
     } 
     iframe { 
      width: 900px; 
      height: 500px; 
     } 
     div.top { 
      background-color: rgba(2, 109, 8, 0.83); 
      height: 30px; 
      width: 100%; 
     } 
    </style> 
</head> 
<body> 
    <div class="top"> 
     <img src="favicon.png" alt="favicon" height="31" width="31"> 
     <h1>Chat</h1> 
    </div> 

    <iframe id="iframe" src="text.txt"></iframe> 

    <script> 
     window.setInterval(function() { 
      reloadIFrame() 
     }, 3000); 

     function reloadIFrame() { 
      console.log('reloading..'); 
      document.getElementById('iframe').contentWindow.location.reload(); 
     } 
    </script> 
</body> 
</html> 

問候

0
<head> 
    <script> 
     function refreshIFrame() { 
      var x = document.getElementById("*Your_iframe_id*"); 
      x.contentWindow.location.reload(); 
      var t = setTimeout(refreshIFrame, 3000); 
     } 
    </script> 
</head> 
<body onload="refreshIFrame()">... 
    <iframe id="*Your_iframe_id*" src= ...></iframe>... 
</body> 

該解決方案爲我工作,我發現'refreshIFrame()'函數是作爲參數輸入的,不帶大括號()在下面的行中:「var t = setTimeout(refreshIFrame,3000);「。只需替換相關的值,你就可以走了。