【问题标题】:How Do I make an Iframe go to a random page every 5 seconds如何让 iframe 每 5 秒转到一个随机页面
【发布时间】:2017-09-27 18:47:29
【问题描述】:

我需要有关 iframe 的帮助。

到目前为止,我已经设置了这段代码

<iframe height="300px" width="100%" src="/vite" name="iframe_a"></iframe>

<button><a href="/multi" target="iframe_a">MULTI</a></button>
<button><a href="/facebook" target="iframe_a">Facebook</a></button>

但是,我不想要按钮。我希望 iframe 每五秒按网页列表的顺序重定向一次,因为:
在它旁边,我有一个幻灯片,当它的徽标出现时,我希望框架重定向到徽标所指示的页面。

示例:

Facebook 徽标 > href="/facebook"
[5 秒后]
Vite logo > href="/vite"
[5 秒后]
多标志 > href="/multi"

我已经有幻灯片设置,只是要求框架。

谢谢大家

【问题讨论】:

  • 您有一些更改幻灯片放映的代码(您没有费心提出问题)。更改该代码,使其同时更改框架。

标签: javascript html iframe


【解决方案1】:

你可以试试这样的。注意:您使用的网站必须包含正确的 iframe 和来源标头(或从与父网站相同的域提供)才能显示在您的 iframe 中

//Array of URLs for the iframe to cycle through
var websites=["https://wikipedia.org","https://stackoverflow.com","https://stackoverflow.com/questions/44497495/how-do-i-make-an-iframe-go-to-a-random-page-every-5-secounds#"], 
//Variable to hold the iframe node reference
iframe, 
//Time to show each website in milliseconds
INTERVAL=5000, 
//Variable to keep track of currentSlide being shown
currentSlide=-1;

//Method to update the SRC attribute of iframe
function switchSlides(){
  currentSlide = (currentSlide+1)%websites.length;
  iframe.src = websites[currentSlide];
}

//Wait for document to load
window.onload=function(){
  //Get iframe reference
  iframe = document.getElementById("slides");
  //Use setInterval to set up repeating task of switching slides
  setInterval(switchSlides,INTERVAL)
}
&lt;iframe id="slides" height="300px" width="100%" name="iframe_a"&gt;&lt;/iframe&gt;

【讨论】:

  • 如果不安全怎么办?如果它不安全,我该如何让它工作?
  • 如果您从上面的代码 sn-p 进行测试(从 https 提供),您将需要使用 https 站点。但是如果你的网站不是 https,你可以使用没有 http 的其他网站
  • 您上面的代码有效,但我的网站不是 https(安全)它们是 http。如何让上面的代码与 http 一起使用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-12
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多