Code:
<script type="text/javascript">
(function(w){
var links = [];
links[0] = "http://www.javascriptkit.com";
links[1] = "http://www.dynamicdrive.com";
links[2] = "http://www.cssdrive.com";
links[3] = "http://www.codingforums.com";
links[4] = "http://www.news.com";
links[5] = "http://www.gamespot.com";
links[6] = "http://www.msnbc.com";
links[7] = "http://www.cnn.com";
links[8] = "http://news.bbc.co.uk";
links[9] = "http://www.news.com.au";
var cook = {
set: function(n, v, d){ // cook.set takes (name, value, optional_persist_days) - defaults to session if no days specified
if(d){var dt = new Date();
dt.setDate(dt.getDate() + d);
d = '; expires=' + dt.toUTCString();}
document.cookie = n + '=' + escape(v) + (d || '') + '; path=/';
},
get: function(n){ // cook.get takes (name)
var c = document.cookie.match('(^|;)\x20*' + n + '=([^;]*)');
return c? unescape(c[2]) : null;
}
};
var arrayindex = (typeof [].indexOf === 'function')? function(ar, item){return ar.indexOf(item);} :
function(ar, item){
var idx = ar.length;
while(--idx > -1){
if(ar[idx] === item){
return idx;
}
}
return -1;
};
w.randomlinks = function (){
var len = links.length, myrandom = Math.floor(Math.random() * len ), stored = cook.get('storedrandompagenums');
if(stored){
stored = stored.split('.');
while(arrayindex(stored, myrandom.toString(10)) > -1){
myrandom = (myrandom + 1) % len;
}
stored.unshift(myrandom);
if(stored.length > 3){
stored.pop();
}
stored = stored.join('.');
} else {
stored = myrandom;
}
cook.set('storedrandompagenums', stored);
w.location.href = links[myrandom];
}
})(window);
</script>
<form>
<input type="button" value="random link!" onclick="randomlinks();">
</form>
NOTES: You no longer need to deal with the number of entries in the links array, the code will calculate it for you. Just add or remove as necessary, just make sure there are more than 3 and that they are numbered sequentially starting at 0. The non-repeating nature of the code (the three last pages will not repeat) relies upon the user having cookies turned on, most do. Any questions, feel free to ask.
Bookmarks