Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Keep last image viewed and start with next image

    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.

    Code:
    <!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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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:-

    Code:
    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.

  • Users who have thanked Philip M for this post:

    dino67 (12-10-2010)

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thanks

    Thank you for your help. That was it. It works great now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •