...

View Full Version : Cookie reading/incrementing/updating



lazycarrot
07-12-2011, 08:26 PM
Novice Javasript query. :o

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




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

Krupski
07-12-2011, 09:52 PM
Solution: Set a cookie....

Try these functions:



/***
* 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...

Logic Ali
07-12-2011, 10:19 PM
if (cookies.indexOf(pookie) == -1)

Since pookie is undefined at this point, that line in itself must generate an error.

lazycarrot
07-13-2011, 12:12 AM
Duplicate Post - sorry

lazycarrot
07-13-2011, 02:58 AM
Thanks for the helping hands.
Have honed the coding down to the following and it's functioning 90% ok:


<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! :confused: it can't be as difficult as i'm making it!
any pointers please?

Logic Ali
07-13-2011, 03:57 AM
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.

lazycarrot
07-13-2011, 09:44 AM
Cheers! Case closed! :D

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum