lazycarrot
07-12-2011, 07: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...
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...