/    Sign up×
Community /Pin to ProfileBookmark

On Click Randomiser

Hi

I am creating an image which, when you click on it, sends you to one of four links. I have managed to code this, but the problem I am having is that it is completely random (only part of the point). What I would like to be able to do is to randomise the first click through, and then if the user goes back to the image, only leave them with the remaining three destinations, and then obviously two and one at the end. This is to stop them theoretically ending up at the same link every single time and not being able to access the other three.

Does anybody know how I might be able to do this? The code I have currently is:

<img src=”IMAGE” onclick=”randomLink();”>

<script type=”text/javascript”>

var randomLink = function () {

var links = [
“LINK 1”,
“LINK 2”,
“LINK 3”,
“LINK 4”,
];

var max = (links.length)
var randomNumber = Math.floor(Math.random()*max);

var link = links[randomNumber];

window.location = “http://” + link;
}
</script>

Thanks in advance

Alex

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@TcobbSep 16.2014 — You might consider storing previously selected links in sessionStorage and having the random selection number as a function. Loop until it comes up with a value that is different from those stored in the sessionStorage variable(s), place that in sessionStorage as well, and then redirect to the previously unvisited link it comes up with.
×

Success!

Help @AlexCrow spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...