3

I'm trying to open build a system which opens a new link every time an anchor tag is clicked. I tried using random function but at times it opens the same link again and again. But whereas as I want the link to open in an order and follow the cycle.. That is if I have three links, the user clicking the anchor text first time will open the first link, then second, then third and later again the first link..

This means the link must change if a different user clicks.

Here is a code that I found which uses random function..

<script type="text/javascript'>
function random_3(){
    var myrandom=Math.round(Math.random()*2)
    var link1="http://www.codingforums.com"
    var link2="http://www.cssdrive.com"
    var link3="http://www.dynamicdrive.com"
    if (myrandom==0)
        window.location=link1
    else if (myrandom==1)
        window.location=link2
    else if (myrandom==2)
        window.location=link3
}
</script>
<form>
<input type="button" value="random link!" onClick="random_3()">
</form>

so can someone help me out with this?

J. Chomel
  • 8,193
  • 15
  • 41
  • 69

5 Answers5

1

I tried using random function but at times it opens the same link again and again

You're probably getting the same link several times in succession due to the random calculation that you're using. Try the following instead:

var myrandom=Math.floor(Math.random()*3);

You should get a better distribution this way.

Now, regarding your original question, if you want a persistent state to be kept between page reloads, so that each time the link is different, you'll probably need to use cookies or localStorage for storing what was the last used index for that user.

Example:

var links = ['http://www.codingforums.com', 'http://www.cssdrive.com', 'http://www.dynamicdrive.com'];
function nextLink() {
 var index = localStorage.getItem('lastPos') || -1;
 index = (index + 1) % links.length;
 localStorage.setItem('lastPos', index);
 return links[index];
}
Ricardo Sousa
  • 178
  • 1
  • 7
0

First of all, you have to list your links in an array. Second, you have to define a variable that hold the last opened link's index in that array, to void a successive opening of the same link (indeed it's a URL). In general you may follow something like the following code:

urls = ['http://domain1.com','http://domain2.com', 'http://domain3.com', 'http://domain4.com'];
var lastUrl = '';
function getRandomIndex(arr){  
  return Math.floor(Math.random() * ((arr.length+1) - 1));
}
function openUrl(){
  url = urls[getRandomIndex(urls)]; 
  do {
      if (url == lastUrl){
        url = urls[getRandomIndex(urls)];
      }
    else{
        lastUrl = url;
        //Using alert instead of window.location for testing
        alert(url)
        //window.location = url;       
    }
  }
  while (lastUrl != url);
  
}

Then for test:

<a href="javascript:openUrl()">Click here for test</a>

This is an online Demo

Reference: Generating random whole numbers in JavaScript in a specific range?

Edit

According your comment, the requirement is pretty easier, instead of choosing according to random value, you will define lastUrl as an integer store and every time the openUrl() is called, it will be increased by 1 after checking its value if it is greater than urls array length or not as follows:

Modified openUrl

urls = ['http://domain1.com','http://domain2.com', 'http://domain3.com', 'http://domain4.com'];
//lastUrl Index in urls array
// Global variable
var lastUrl = 0;
function openUrl(){
      if (lastUrl > (urls.length - 1)){
          //reset it to 0
          lastUrl = 0;
      }
      else{
          url = urls[lastUrl];
          lastUrl++;
          alert(url)
        //window.location = url; 
      }
    }
Community
  • 1
  • 1
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
0

What you need to do is save the index of the link, and then increase it every time so that you cycle through the links in order.

Here's a working example of what you want:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script>
  urls = ['http://domain1.com','http://domain2.com', 'http://domain3.com', 'http://domain4.com'];
  var urlIndex = 0;

function openUrl(){
  url = urls[urlIndex % urls.length];
  window.location = url;
  urlIndex ++;
}
  </script>
  <a href="javascript:openUrl()">Get a link</a>
</body>
</html>

Here's a demo :)

  • sorry, maybe I was not clear before..for example if 3 users are coming to my website..then when user 1 clicks this link he goes to link 1, similarly user 2 goes to link2 and user 3 goes to link 3 and after that when the fourth user comes and clicks the link..he again goes to link 1 and so on..and if im not wrong, the above code will be applicable to the same user.. – Patricia Moore May 17 '16 at 18:16
  • 1
    "user 1 clicks this link he goes to link 1, similarly user 2 goes to link2 and user 3 goes to link 3" This can be done on server side only. Excluding concurrency problem. No client side solution is possible. – Alex Kudryashev May 17 '16 at 19:36
0

 var myrandom = 0;

function random_3(){
   myrandom += 1;
    
    var link1="http://www.codingforums.com";
    var link2="http://www.cssdrive.com";
    var link3="http://www.dynamicdrive.com";
    if (myrandom==1)
      window.open('http://www.codingforums.com','_blank');
    else if (myrandom==2)
         window.open('http://www.cssdrive.com','_blank');
    else if (myrandom==3)
        window.open('http://www.dynamicdrive.com','_blank');
    else if (myrandom>3)
         window.open('http://www.codingforums.com','_blank');
}
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<form>
<input type="button" value="random link!" onClick="random_3()">
</form>
Akram Khan
  • 114
  • 8
-1

You can count click like user click on button and basis on this manage.

Akram Khan
  • 114
  • 8