在特定时间后使用JavaScript将iFrame刷新到不同的源

Refresh iFrame to different source using JavaScript after a specific time

本文关键字:刷新 iFrame 定时间 JavaScript      更新时间:2023-09-26

我最初想在iframe中加载一些网站。1分钟后,JavaScript会将嵌入的页面刷新到另一个页面。这就是我想到的。

<html>
<body>
    <IFRAME src="http://firstwebsite.com" width="100%" height="100%" frameBorder="0" id=”myframe”></IFRAME>
    <script>
        setTimeout("document.getElementById('myframe').src = 'http://secondwebsite.com';",6000);
</script>
</body>
</html>

但这不起作用。请帮我修改这个相同的脚本,而不是给我新的想法。

您的代码中有两个错误:

  1. 将iframe id中的更改为'"
  2. 在1分钟内使用60000,而不是6000

休息可以。以下是编辑:

<html>
<body>
    <IFRAME src="http://firstwebsite.com" width="100%" height="100%" frameBorder="0" id="myframe"></IFRAME>
    <script>
        setTimeout("document.getElementById('myframe').src = 'http://secondwebsite.com';",60000);
</script>
</body>
</html>

您应该编写不同的setTimeout。

<html>
<body>
    <IFRAME src="http://firstwebsite.com" width="100%" height="100%" frameBorder="0" id=”myframe”></IFRAME>
    <script>
        setTimeout(function(){document.getElementById('myframe').src = 'http://secondwebsite.com';},6000);
</script>
</body>
</html>

尝试setTimeout(function() { document.frames['myframe'].src = .... }, 6000);

- Change ” to ' or " in the iframe id that you have used.
(id's can only be picked up by " or '.)
- and finally 60000millisec = 60sec = 1min.

检查小提琴:

如果你想看看的话,刚刚把时间更新到5000。(你只需要等5秒(

基本上这是短代码:

  setTimeout("document.getElementById('myframe').src = 'http://secondwebsite.com';",60000);