The first time the page is loaded a list of randomly selected urls is created from the main array (urlArray) and the link set to go to the first url in the list. Each time the page is reloaded the link is automatically set to go to the next url in this list, once all the urls have been used a new list of randomly selected urls is created.
The numbers shown represent the index number in the urlArray.
|
The order of the current randomly selected numbers are The current index number being referenced is The value at urlArray[] is The link will read <a id="rdmlnk" href="" onclick="frameChk()">Link</a> |
|
Each urlArray index allows for 2 entries, the url and the url description
urlArray[n]=new Array("url","Url Description")
The script is set to use the Url Description by default as stated by variable linkDesc having empty quotes linkDesc=""If you want the same description for all the links then leave the URL Description entry blank
urlArray[n]=new Array("url","")
and enter your link description at variable linkDesc linkDesc="Random Link"
By changing the value of variable howMany=urlArray.length to a number less than the length of urlArray you can set the script to only select that number of urls.
howMany=5 will only randomly select 5 urls from the urlArray
framed = 1
and the value of frameName set to the name of the target frame.
frameName="target frame name"
To have the links open in a new browser window set the variable framed to 1 and frameName to null
The script works by dynamically creating a second array whose index values are populated with randomly selected index numbers from the urlArray.
This second array is then referenced in numerical order using its index values to get the appropriate url in the main array.
The variable count represents the number of urls that have been used.
A cookie is used to save the contents of the second array and the variable count, when the page is reloaded the script checks to see if a cookie exists and if it does the second array is recreated using the values in the cookie with the value of variable count being implemented to set the next link in the list. If there is no cookie a new list is created.
When the cookie is loaded a check is also carried out to make sure that the number of indexes in the cookie matches the number of indexes selected by the setting of variable howMany if there is a mismatch then the cookie is deleted and a new randomly selected list is created.
This would only happen if the value of howMany is changed and ensures that the user always gets to use your latest list.
Using the cookie in this way means that should your visitor not use all the links in the list the next time they visit the links would carry on from where they left off, unless they deleted the cookie on their system or you change your list.
<script type="text/javascript">
<!--
// Realised by apachejeff
//www.huntingground.freeserve.co.uk
urlArray=new Array()
urlArray[0]=new Array("url1","Link Text 1")
urlArray[1]=new Array("url2","Link Text 2")
urlArray[2]=new Array("url3","Link Text 3")
urlArray[3]=new Array("url4","Link Text 4")
urlArray[4]=new Array("url5","Link Text 5")
urlArray[5]=new Array("url6","Link Text 6")
urlArray[6]=new Array("url7","Link Text 7")
urlArray[7]=new Array("url8","Link Text 8")
urlArray[8]=new Array("url9","Link Text 9")
urlArray[9]=new Array("url10","Link Text 10")
linkDesc="" // "" = use url description in the array, or enter a description between quotes that will be used for all links
framed=0 // 0 = no 1 = yes
frameName="frame_name" // frame name
totalRange=urlArray.length // numbers range to select from
howMany=urlArray.length // how many to select
function generate(){
total=urlArray.length
oSelect=howMany
numbersRange=new Array() // array to hold numbers to select from.
selectedNum=new Array() // array to hold selected numbers
for(n=0;n<total;n++){ // create list of numbers to choose from
numbersRange[n]=n // start of numbers to choose from
}
for(p=0;p<oSelect;p++){ // generate a random number for oSelect times
rndnum=Math.floor(Math.random()*total)
selectedNum[p]=numbersRange.splice(rndnum,1) // add to selectedNum array and remove from numbersRange array
total--
}
showSelectedNumber()
}
count=0
function showSelectedNumber(){
if(count==selectedNum.length){
count=0
generate()
return
}
document.getElementById("rdmlnk").href=urlArray[selectedNum[count]][0] // link target
document.getElementById("rdmlnk").innerHTML=(linkDesc==""?urlArray[selectedNum[count]][1]:linkDesc) // link text
count++
}
function frameChk(){
if(framed==1){
setTimeout("showSelectedNumber()",100)
document.getElementById("rdmlnk").target=frameName
}
}
cookieName="random_urls"
expdays=365
// An adaptation of Dorcht's cookie functions.
function setCookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure")
}
function getCookie(name){
var arg = name + "="
var alen = arg.length
var clen = document.cookie.length
var i = 0
while (i < clen){
var j = i + alen
if (document.cookie.substring(i, j) == arg){
return getCookieVal(j)
}
i = document.cookie.indexOf(" ", i) + 1
if (i == 0) break
}
return null
}
function getCookieVal(offset){
var endstr = document.cookie.indexOf (";", offset)
if (endstr == -1)
endstr = document.cookie.length
return unescape(document.cookie.substring(offset, endstr))
}
function deleteCookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT"
}
function saveDataToCookie(){
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000)); // expiry date
path=null // path="/" top directory, path=null this documents root directory
data=selectedNum+","+count
setCookie(cookieName,data,expdate,path)
}
function getCookieData(){
inf=getCookie(cookieName)
if(!inf){
generate()
return
}
cookieData=inf
cookieData=cookieData.split(",")
if((cookieData.length-1)!=howMany){
deleteCookie(cookieName)
generate()
return
}
selectedNum=new Array()
for(i=0;i<cookieData.length-1;i++){
selectedNum[i]=cookieData[i]
}
count=cookieData[cookieData.length-1]
showSelectedNumber()
}
// add onload="getCookieData()" onunload="saveDataToCookie()" to the opening BODY tag
// -->
</script>
<a id="rdmlnk" href="#null" onclick="frameChk()">Link</A>