...

View Full Version : Keep last image viewed and start with next image



dino67
12-09-2010, 11:08 PM
I'm new to javascript. I'm trying to make a script to load one of a series of images, as hyperlinks, in order, not randomly, on each page load of my site homepage. In addition, I need the script to do the following: if the user first visits today he gets the first image and if he returns to the homepage later today or any other time in the future, the series of images loaded begins with the next image in the series. In other words if there are a series of 4 images, he sees one image each visit to the homepage, in order, and the sequence begins over again once he has viewed all 4 images, regardless of when that is.

I found some code on the 'net that loads images in sequence on each page load, but it only works with the current viewing session, and once the viewer leaves the site the last image they viewed is not kept and the sequence starts from the beginning instead of where they left off. The code is below, but I can't figure out how to modify it to do what I want. Any help would be greatly appreciated.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<noscript><img src="images/Image1.png"></noscript>
<script type="text/javascript">
function robin(){
//Set Your Images in the below Array:
var robin_im=["images/Image1.png", "images/Image2.png", "images/Image3.png", "images/Image4.png"]

//set the links in the below Array:
var robin_link=['http://www.yahoo.com', 'http://www.google.com', 'http://www.cnn.com', 'http://www.msnbc.com']

//////////////// Stop Editing //////////////////
function createCookie(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=/";
}

function readCookie(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;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}
var num=readCookie('robim')? readCookie('robim')*1 : 0;
document.write('<a href="'+robin_link[num]+'"><img src="'+robin_im[num]+'" border=0></a>')
num=num<robin_im.length-1? num+1 : 0;
createCookie('robim', num);
}
robin();
</script>

</body>
</html>

Philip M
12-10-2010, 01:29 PM
The reason why it is a session cookie and not a persistent cookie is that you have not specified the number of days to persist:-


createCookie('robim', num, 10); // 10 days persistence - change to suit



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

dino67
12-10-2010, 06:30 PM
Thank you for your help. That was it. It works great now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum