Javascript赢得了autorefresh iframe

时间:2015-09-03 19:05:57

标签: javascript html

我希望iframe每隔5秒刷新一次,但它不起作用,我不明白为什么?我手动更改了文本文件,但它不会自动刷新网站。 文本文件有什么特别之处吗?



<!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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您可以通过alert()替换整个函数来调试问题,只是为了查看间隔是否正常工作。

还将<script>更改为<script type="text/javascript">

答案 2 :(得分:0)

这将起作用 - (注意)用于演示目的我已经将你的时间间隔改为超时,因此它只会发生一次。并且为了使重新加载在视觉上显而易见,我在您的问题中添加页内导航哈希值。

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;