How Do I make an Iframe go to a random page every

2019-09-15 03:25发布

问题:

I need help with an iframe.

So far, I have this code set up

<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>

However, I don't want the buttons. I want the iframe to redirect in order of a list of web pages every five seconds because:
Next to it, I have a slideshow, and when its logo appears, I would like the frame to redirect to the page of which the logo is indicating.

Example:

Facebook logo > href="/facebook"
[5 seconds later]
Vite logo > href="/vite"
[5 seconds later]
MULTI logo > href="/multi"

I already have the slideshow setup, just asking for the frame.

Thanks heaps

回答1:

You can try something like this. Note: The websites you use must include the correct iframe and origin headers (or be served from the same domain as the parent site) to display inside your 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)
}
<iframe id="slides" height="300px" width="100%" name="iframe_a"></iframe>