PDA

View Full Version : Can make Random Image Link NOT Random?


scrapoetry
08-02-2005, 04:49 PM
Hi, I'm using the Random Image Link script and it works great except I don't want the image to be random. I've searched and searched and can't find what I'm looking for. (Maybe I don't know the right words to search with)

Anyway, I want the image to pull in the order they are placed in the array, not randomly. Can this script be changed or maybe another script to do this?

Script url:
http://www.javascriptkit.com/script/cut144.shtml

My site url:
http://www.scrapoetry.com/topsites/
Banner images after every 10 sites listed is where I have the images but they are random and sometimes one image is seen more than another, I'd like them to be evenly distributed.

THANKS!!!

-Rachel

jscheuer1
08-02-2005, 05:34 PM
At that rate, until you get more images than available slots for them, why not just allocate one image per slot and forget about randomizing? Once you get more images than slots, you can begin randomizing at the top or bottom slot. The more images, the more likely a given random image will differ from another. With enough images, all slots can be randomized without much likelihood of too much repetition.

Mr J
08-02-2005, 10:24 PM
You have 5 of these image links in your page, are you wanting the same image link to be shown in each instance?

The following example will show the same image link 5 times.
Each time the page is loaded the next image in the array is shown.

In order to iterate through all your images a cookie is used to remember which image was shown the last time the page was loaded otherwise you would only ever see the first image.

You do not copy the script 5 times into your page it is put in the HEAD section and the image tags place in your page where you require them.

The following example is a stand alone page so that you can see how it works.

The image and url information is entered into a 2 dimensional array.


<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script type="text/javascript">
<!--
// Jeff
// www.huntingground.freeserve.co.uk

cookie_name="seq_links"
expDays=365

myimages=new Array()
myimages[myimages.length]=new Array("/topsites/images/banner_angelic_crafts4.jpg","http://www.scrapoetry.com/topsites/out.php?id=253")
myimages[myimages.length]=new Array("/topsites/images/banner_blinkie1.gif","http://www.scrapoetry.com/topsites/index.php?a=ads")
myimages[myimages.length]=new Array("/topsites/images/banner_blinkie2.gif","http://www.scrapoetry.com/topsites/index.php?a=ads")

function show_pic() {
count = get_cookie(cookie_name)
if(count==null ||count == myimages.length ){
count=0
}

for(i=0;i<5;i++){
document.getElementById("mypic"+i).src=myimages[count][0]
}
count++
}

function save_pic_count(){
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
set_count=count
set_cookie(cookie_name, set_count, exp);
}

function set_cookie(name, value, expires, path, domain, secure) {// An adaptation of Dorcht's function for setting a cookie.
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 get_cookie(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 get_cookie_val (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function delete_cookie(name,path,domain) { // An adaptation of Dorcht's function for deleting a cookie.
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT";
}

function newWin(){

myWin=window.open(myimages[count][1])

}

// add onload="show_pic()" onunload="save_pic_count()" to the opening BODY tag

// -->
</script>


</HEAD>
<BODY onload="show_pic()" onunload="save_pic_count()">

<img id="mypic0" src="" onclick="newWin()">

<img id="mypic1" src="" onclick="newWin()">

<img id="mypic2" src="" onclick="newWin()">

<img id="mypic3" src="" onclick="newWin()">

<img id="mypic4" src="" onclick="newWin()">


</BODY>
</HTML>