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 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Cookie reading/incrementing/updating

    Novice Javasript query.

    My requirement: Each time a visitor arrives at a specific webpage i want the main image on the page to be different, or more specifically, one of 6 images which will be shown in rotation.

    Solution: Set a cookie. Each time the visitor access the page read the cookie and display the corresponding image. Then increment the value and rewrite the cookie, so that next time they'll see the next image in sequence.
    NB: if the cookie does not exist (first timer) or is at 6, then the value is set to zero (and then incremented).

    Problem: Can't get my coding to work. Specifically it just doesn't do anything - no error message, no cookie written.
    I'm a very novice scripter, as in I've cobbled the coding together from bits off the net that i think i've managed to grasp some kind of an understanding of.
    Very suck it and see - so far lots of sucking and no seeing!

    Anyway here's my code - if someone could cast a beady eye it would be much appreciated. Hopefully I'm not a million miles out !!!

    Code:
    <script type="text/javascript"> 
    /*<![CDATA[*/
    
    	var cookies = document.cookie;
    	if (cookies.indexOf(pookie) == -1)
    	{
    		var pookey = 0;
    	}
    	else
    	{
    		var startpos = cookies.indexOf(name)+name.length+1;
    		var endpos = cookies.indexOf(";",startpos)-1;
    		if (endpos == -2) endpos = cookies.length;
    		var pookey = cookies.substring(startpos,endpos);
    		if (pookey == 6) pookey = 0;
    	}
     
     	pookey = pookey++;
    	
    	var expire = new Date ();
    	expire.setTime(expire.getTime() + 1000 * 60 * 60 * 24 * 365);
    
    	document.cookie = name + "=" + pookie + "; expires=" + expires.toGMTString() + "; path=/";
      
      if (pookey == 1) document.write ("<img src=\"../HomePage/Image1.jpg\"  />");
      if (pookey == 2) document.write ("<img src=\"../HomePage/Image2.jpg\"  />");
      if (pookey == 3) document.write ("<img src=\"../HomePage/Image3.jpg\"  />");
      if (pookey == 4) document.write ("<img src=\"../HomePage/Image4.jpg\"  />");
      if (pookey == 5) document.write ("<img src=\"../HomePage/Image5.jpg\"  />");
      if (pookey == 6) document.write ("<img src=\"../HomePage/Image6.jpg\"  />");
      
    /*]]>*/
    </script>
    Thanks in advance for any help...

  • #2
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by lazycarrot View Post
    Solution: Set a cookie....
    Try these functions:

    Code:
    /***
     * write a cookie: name, value, lifetime in days
     **/
    var writeCookie = function (name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 86400000));
            var expires = '; expires=' + date.toGMTString();
        } else {
            expires = '';
        }
        var content = name + '=' + value + expires;
        document.cookie = content;
        return content;
    }
    
    /***
     * read a cookie: name returns value or false if not found
     **/
    var readCookie = 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 false;
    }
    Should be easy to take it from here...
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • Users who have thanked Krupski for this post:

    lazycarrot (07-13-2011)

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by lazycarrot View Post

    Code:
    	if (cookies.indexOf(pookie) == -1)
    Since pookie is undefined at this point, that line in itself must generate an error.

  • Users who have thanked Logic Ali for this post:

    lazycarrot (07-12-2011)

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Duplicate Post - sorry
    Last edited by lazycarrot; 07-13-2011 at 02:00 AM. Reason: Duplicate Post

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks for the helping hands.
    Have honed the coding down to the following and it's functioning 90% ok:

    Code:
    <script type="text/javascript"> 
    /*<![CDATA[*/
    
    	var cookies = document.cookie;
    	if (cookies.indexOf('pookie') == -1)
    	{
    		var pookey = 0;
    	}
    	else
    	{
    		var keypos = cookies.indexOf('pookie')+7;
    		var pookey = cookies.substring(keypos,keypos);
    		if (pookey == 6) pookey = 0;
    	}
    
        ++pookey;
    
    	var expire = new Date ();
    	expire.setTime(expire.getTime() + 1000 * 60 * 60 * 24 * 365);
      
    	document.cookie = "pookie=" + pookey + "; expires=" + expire.toGMTString() + "; path=/";
      
    
      
    /*]]>*/
    </script>
    The problem is in incrementing the variable pookey.
    currently coded as: ++pookey;
    but I've tried umpteen other permutations - none of which will add 1 to the damn thing! it can't be as difficult as i'm making it!
    any pointers please?

  • #6
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    var pookey = cookies.substring(keypos,keypos);
    If the start and end indices are the same, you get a null string, which when incremented will equal 1.

  • Users who have thanked Logic Ali for this post:

    lazycarrot (07-13-2011)

  • #7
    New Coder
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Cheers! Case closed!

    These forums are awesome and really appreciated.
    It's a real help to just feel not totally isolated - you can look at a problem until you're blue in the face. A friendly nudge in the right direction makes all the difference.

    My thanks again.
    Last edited by lazycarrot; 07-13-2011 at 02:07 PM. Reason: hate it when i spell "your" instead of "you're" !!!! :)


  •  

    Posting Permissions

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