Reputation: 1
I got a script that opens links randomly ... but I needed to know how I create a script to open a link by myself and then return to the initial one. Eg... Click on the button and it opens link1. Click it again and it opens link2 ... again and opens link3 .... then opens link4 .... and then opens link1 again. I'm using this script to make him open the link randomly .... but I need it to be sequential and after opening the last link he opens link1 again. Can you help me guys?! Best regards! Alex.
<script>
<! -
/ *
Random Links Button
* /
// Specify the links to work at random below. You can enter as many as needed
var randomlinks=new Array()
randomlinks[0]="https://website1.com"
randomlinks[1]="https://website2.com"
randomlinks[2]="https://website3.com"
randomlinks[3]="https://website4.com"
function randomlink(){
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
}
// - END OF SCRIPT-
</script>
Upvotes: 0
Views: 719
Reputation: 416
You can do this using window.open and by iterating the variable value on the button and pass the it as array index
<button onclick="randomlink()" >Random Link</button>
<script>
Links = []
Links.push("https://alloytech.wordpress.com");
Links.push("https://www.google.com/");
Links.push("https://www.youtube.com/");
Links.push("https://www.stackoverflow.com/");
var randomParam=0;
function randomlink() {
randomParam = randomParam >= Links.length?0:randomParam;
window.open(Links[randomParam],'popUpWindow','height=400,width=800,scrollbars=yes');
randomParam++;
}
</script>
I think this is what you are expecting
Upvotes: 0
Reputation: 33803
You should be able to modify a global variable, which acts as a click counter, from within the function. eg:
var randomlinks=new Array();
randomlinks[0]="https://website1.com"
randomlinks[1]="https://website2.com"
randomlinks[2]="https://website3.com"
randomlinks[3]="https://website4.com"
var i=0;
function clickroundrobin(e){
if( i >= randomlinks.length )i=0;
var url=randomlinks[i];
i++;
window.location=url;
}
To test:
var randomlinks=new Array();
randomlinks[0]="https://website1.com"
randomlinks[1]="https://website2.com"
randomlinks[2]="https://website3.com"
randomlinks[3]="https://website4.com"
var i=0;
function clickroundrobin(e){
if( i >= randomlinks.length )i=0;
var url=randomlinks[i];
i++;
//window.location=url;
alert( url )
}
<a href='#' onclick='clickroundrobin(event)'>Click</a>
Upvotes: 1