0

I am working on a project - When the URL to my site (www.mywebsite.com), is entered, I want it to go automatically go to a different website in the same browser window and then go another website (www.anotherWebsiteOne.com) in the same and the after X seconds will load another webSite (www.anotherWebSiteTwo.com) in the same browser, and so on.

I would like everytthing to stay in the same browser window


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

   <title>Basic Javascript Example</title>

   <script type="text/javascript" src="webSites.js"></script>
   
</head>

<body onload="start()">  

</body>

</html>

and my webSites.js:

var webSites = [
    'http://www.anotherWebPageOne.com/',
    'https://www.anotherWebPageTwo.com/',
    'http:www.anotherWebPageThree.com/',
];

var iTarget;

function nextTarget(){
    window.open( targets[iTarget], 'target' );
    if( ++iTarget >= targets.length ) {
        iTarget = 0;
    }
}

function start() {
    iTarget = 0;
    nextTarget();
    setInterval( nextTarget, 5000 );
}

start() 

3 Answers 3

1

you can use :

<body onload="start()">  

</body>

or you can simply add start() to the end of your js file

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Amir - this worked, I have made anl edit to my op..any help would be great
0

just add start() to end of your webSites.js file

whatever in your js file will be executed automatically and you have to just call the start function in the js file by invoking it.

also, rename your webSites variable to targets.

Comments

0

in your JS file you just need to add window.onload = <Your Function Name>;
eg:-

var webSites = [
    'http://www.anotherWebPageOne.com/',
    'https://www.anotherWebPageTwo.com/',
    'http:www.anotherWebPageThree.com/',
];

var iTarget;

function nextTarget(){
    window.open( targets[iTarget], 'target' );
    if( ++iTarget >= targets.length ) {
        iTarget = 0;
    }
}

window.onload = nextTarget;

function start() {
    iTarget = 0;
    nextTarget();
    setInterval( nextTarget, 5000 );
}

I hope it will solve your problem

2 Comments

Thanks Sukarna, but this did not work - no page loaded.
Try out this :- window.onload = function(){ // your code here };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.