Using iFrames to switch sources repeatedly

I have created an iFrame and I am looking to dynamically change its source to one of 10 different URLs each time it loads:

Here is the desired behavior:

iFrame loads => change to example.com => iFrame loads => change to example1.com => iFrame loads => change to example2.com...

Is there a way to achieve this functionality? I am currently stuck on how to proceed and would appreciate any guidance.

<html>
<body>
<iframe src="http://www.example.com"> </iframe>
<script>   
var links = [
"http://www.js.com", 
"http://example.com", 
"http://example1.com"];

alert(links[2])

function srca (){
document.getElementsByTagName('iframe')[0].src = links[0]
}


</script>
</body>
</html>

Answer №1

It seems like you were close, but you just needed to remember to connect to an event (such as onload)..
To help you out, here is a starting point:

<html>
<head>
<script>   
var links = [ 'http://www.js.com'
            , 'http://example.com' 
            , 'http://example1.com'
            ]
,  lnkCnt = 0
;
function srca(){
   links.length > lnkCnt && (
     document.getElementsByTagName('iframe')[0].src = links[lnkCnt++]
   );
}
</script>
</head>
<body>
<iframe src="about:blank" onload="setTimeout(srca, 2000);"></iframe>

</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular JS has the capability to toggle the visibility of elements on a per-item basis as well as

I have created a repeater that displays a headline and description for each item. I implemented a checkbox to hide all descriptions at once, which worked perfectly. However, I also wanted to allow users to hide or show each description individually. I almo ...

Issue of Vue not resolving Ionic back button component

I'm having trouble figuring out why Vue is giving me a warning and the back button isn't functioning as expected: runtime-core.esm-bundler.js?5c40:38 [Vue warn]: Failed to resolve component: ion-back-button at <TheHeader titulo="Home&q ...

Techniques for manipulating multiple circles with JavaScript

After creating a series of circles with d3, I successfully implemented the functionality to drag individual circles and track their positions. var width= 800; var height=600; svg= d3.select("body").select(".div1").append("svg") ...

Inquiries about utilizing setTimeout with backbone.js and effectively managing timeouts

One of the questions I have is related to clearing timeouts using clearTimeout(content.idTimeout) for a specific idTiemout. But how can I clear all timeouts at once? Here is the model I am working with: var ContentModel = Backbone.Model.extend({ URL: "htt ...

Cease / Cancel Ajax request without activating an error signal

I am looking for a way to intercept all ajax requests on a page and stop/abort some of the requests based on certain criteria. Although initially using jqXHR.abort(); worked, it caused the error event of all the aborted requests to be triggered, which is n ...

Error message: Unexpected token "(" in the asynchronous aspect of Meteor

Currently running meteor version 1.5.1, I am facing a bug while attempting to import an npm module (kraken-api) on the server side: import KrakenClient from 'kraken-api'; > W20170726-22:02:48.177(2)? (STDERR) packages/modules.js:677 ...

using absolute URLs for image source in JavaScript

How can I output the absolute URLs of images in a browser window (e.g., 'www.mysite.com/hello/mypic.jpg')? Below is a snippet of my code: $('img').each(function() { var pageInfo = {}; var img_src = $(this).attr('s ...

Showing text on an ajax loader

While making an ajax call, I have implemented functions that are called on success. To enhance user experience, I am displaying a spinner during the call and hiding it once completed. My goal is to show a message along with the spinner to indicate which fu ...

What causes the selection of the menu item $invalid to always return as true?

I have an HTML form with a select menu: <form name="form"> <select name="gender" ng-model="model" ng-required="true" ng-model-options="{ updateOn: 'mousedown blur'}"> <option value="" disabled>--Select--< ...

Getting started with JavaScript arguments can be overwhelming for newcomers

Can you figure out why the variable 'a' doesn't increase by 1 in the following code snippet? var a = 5; function abc(y) { y++; } abc(a); // The value of 'a' remains 5, instead of increasing to 6. Why is that? However, when ...

Restricting JQuery pop-up to be shown only once

I'm looking for a solution to have a pop-up appear only once when the mouse leaves the screen. I'm not very experienced with coding, so I'm struggling to figure out how to achieve this. // Implementing Exit Intent function addEvent(obj, ev ...

Uploading and saving data to an object in FaunaDB using React hook forms

I am currently facing an issue with uploading/saving data to an object in FaunaDB. Specifically, I am trying to upload a string to a jobProfile object: data: { "jobProfile": { "image": "" "coverImage": " ...

What is the best way to distinguish the compiled files from the source code, while still being able to test and view Express views directly from the source?

I am embarking on a new project using node. I have chosen to organize my directory structure by keeping all source files under ./src and the files intended for server upload under ./dist. The semi-complete directory layout is displayed below. Once built, t ...

How long is a 2D array in JavaScript after adding an element?

My 2D array in Javascript always ends up with a length of 0 when I try to push values into it from within a for loop. It remains empty regardless of my attempts. The issue arises because I am unsure about the number of devices that will be stored in mapLi ...

Interested in transforming and showcasing dates within a MySQL database?

Here's a form with 10 JQuery UI date pickers set up: $(function() { $("#datepicker").datepicker({ minDate: 'today', maxDate: "+90D", showOn: "button", buttonImage: "images/calendar-new2.jpg", buttonImageOnly: true, dateFormat: "D, dd M, yy" ...

Tips for displaying website preloader just once

I recently added a preloader to my website, but I'm having trouble getting it to only play once per visit. Ideally, I want the animation to show up when the site is opened in a new tab or browser window, but not when navigating through the domain by c ...

transferring data from JavaScript on the client side to Express on the server side

Using JavaScript, I have created the HTML DOM that you see in the code below. Now, my goal is to send the value of an input created in JavaScript to the server side. The server is built with node.js and express framework. Once the value is sent to the serv ...

Create a word filter that doesn't conceal the words

I have a code snippet that filters a JSON object based on name and title. I also have an array of specific words and I would like to modify the filter to highlight those words in the text without hiding them. $scope.arrayFilter=["bad,bill,mikle,awesome,mo ...

There will be no pop-up notification displayed if the product is already in the cart

When a product is added to the cart, the following code is used: addproduct(itemId) { this.showLoading = true this.$http.post('/shampoo', {'item': itemId}).then((response) => { swal({ title: "Success!", ...

Empty area beneath the body in Chrome Device Mode

Can anyone explain why there is a strange blank space under the body tag in mobile view on Chrome (other browsers like Firefox and Internet Explorer display it correctly)? Here is the simple code that I am using: <html> <meta name="viewport" con ...