This should work out:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Random Image-Link w/Cookie - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
table {
margin:auto;
}
td {
border:1px solid #222;
}
/* Random Image-Link w/Cookie Styles */
/* common style for javascript and non-javascript */
.ads { /* set to width and height of image(s) */
width:100px;
height:50px;
}
.ads img {
border:0;
display:block;
width:100px; /* set to width and height of image(s) */
height:50px;
}
/* javascript only */
.ads #randomLnk {
visibility:hidden; /* keeps image from displaying empty image token or alt value until loaded */
}
/* non-javascript only */
#ads {
display:none; /* keeps javascript image division from showing when javascript is disabled */
}
/* End Random Image-Link w/Cookie Styles */
</style>
<script type="text/javascript">
/* Random Image-Link w/Cookie ©2008 John Davenport Scheuer
As first seen in http://www.dynamicdrive.com/forums/
username:jscheuer1 This Notice Must Remain for Legal Use
*/
var rpic = {
imgLnks:[ /* 'Image', 'Link', */
'images/assoc1.jpg', 'http://www.42below.com',
'images/assoc2.jpg', 'http://www.bloomsberry.com',
'images/assoc3.jpg', 'http://www.stolengirlfriendsclub.com' //NOTE - no comma after last link entry
],
imgId:'randomPic', lnkId:'randomLnk', divId:'ads',
///////////////////// Stop Editing /////////////////////
diag:false, // default - false (use 'alert' to track selected index numbers, 'kill' to kill the cookie, or 'alert kill' for both)
prep:function(){
document.write('<style type="text/css">#'+rpic.divId+' {display:block;}<\/style>');
},
reveal:function(){
document.getElementById(rpic.lnkId).style.visibility='visible';
},
doit:function(){
var r=rpic, n=Math.floor(Math.random()*(r.imgLnks.length/2))*2;
if(/alert/.test(r.diag))
alert(n+' '+r.taste('randomNum')); // Diagnostic use only
if(r.taste('randomNum'))
while(r.sift(n))
n=n<=r.imgLnks.length-4? n+2 : 0;
if(/alert/.test(r.diag))
alert(n); // Diagnostic use only
document.getElementById(r.imgId).onload=r.reveal;
document.getElementById(r.imgId).src=r.imgLnks[n++];
document.getElementById(r.lnkId).href=r.imgLnks[n--];
r.bake('randomNum', (r.taste('randomNum')? r.taste('randomNum')+'::'+n : n))
if(/kill/.test(r.diag))
r.eat('randomNum'); // Diagnostic use only
},
sift:function(n){
var r=rpic, na=r.taste('randomNum').split('::');
if(na.length*2>=r.imgLnks.length){
r.bake('randomNum', (na=r.taste('randomNum').replace(/^.*::(\d+)$/,'$1')));
na=[na];
na.length=1;
}
for (var i = na.length-1; i > -1; --i)
if(na.length*2<r.imgLnks.length && n==na[i])
return true;
return false;
},
bake:function(name,value,days){
if (days){
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = '; expires='+date.toGMTString();
}
else var expires = '';
document.cookie = name+'='+value+expires+'; path=/';
},
taste:function(name){
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
},
eat:function(name){
rpic.bake(name,'',-1);
}
};
rpic.prep();
window.onload=rpic.doit;
</script>
</head>
<body>
<table>
<tr>
<td>Contents</td>
<td>Contents</td>
<td>Contents</td>
<td>Contents</td>
<td>Contents</td>
<td><noscript><div class="ads"><a href="http://www.42below.com"><img
src="images/assoc1.jpg" alt="banner image" title=""></a></div></noscript>
<div class="ads" id="ads"><a id="randomLnk" href="#"><img
id="randomPic" src="" alt="random banner" title=""></a></div></td>
</tr>
<tr>
<td>Contents</td>
<td>Contents</td>
<td>Contents</td>
<td>Contents</td>
<td>Contents</td>
<td>Contents</td>
</tr>
</table>
</body>
</html>
Bookmarks