Forum Moderators: open

Message Too Old, No Replies

Reading a cookie using javascript, then acting upon it's existance

Trying to read a cookie every 30 seconds, then act upon it's existance

         

blackheart

2:42 pm on May 22, 2005 (gmt 0)

10+ Year Member



I'm trying to read a cookie every 30 seconds, then acting on whether the cookie is set, I want to show an image using the value of the cookie, or a button, which opens a popup. I have this code, but it doesn't work:

function getCookie(name)
{
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin!= 0) return null;
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}

function timedRead()
{
if (getCookie('thumburl'))
var thumburl = getCookie('thumburl');
document.write('<img src=\"' + thumburl + '\" alt=\"Current Image\" />');
} else {
document.write('<input type=\"button\" class=\"submit\" value=\"Get image &raquo;\" onClick=\"::opens window::\">');
}
}

setInterval(timedRead(),30000);

Hopefully someone will see where I have problems, the getCookie script came from the internet, and is as I found it...
I'm setting the thumburl using PHP in the popup, which would be linked to if the cookie didn't exist.

Any help is appreciated :)

Bernard Marx

3:14 pm on May 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



setInterval(timedRead(),30000);

This bit is wrong.

Either:

setInterval("timedRead()",30000);

Or:

setInterval(timedRead,30000); //(compat probs with v. old browsers)

Why are you reading the cookie every 30 secs? Is it likely to change?

blackheart

3:21 pm on May 22, 2005 (gmt 0)

10+ Year Member



It's because it's not set when the page loads, else I would have used PHP to read the cookie, stick to what I know! The cookie is set in the popup window...