That guy
That guy

Reputation: 339

Why wont this javascript work?

I'm trying to detect the existence of a specific cookie "abc". The code that I'm using for this is

var iterations = 0;
var interval = setInterval(checkCookie, 1000);
var cookie=getCookie("abc");
function checkCookie() {
    iterations++;    
    if (iterations >= 3 || cookie !=null || cookie !="")
        clearInterval(interval);
    alert("Iteration " + iterations );
}

The only problem with this code is that it doesn't detect the existence of a cookie. Even when I change the cookie name to one that already exists.You can check it out here http://jsfiddle.net/aMZj3/

Upvotes: -1

Views: 109

Answers (2)

Somnath
Somnath

Reputation: 3275

Just a Note: As per my knowledge if you are working under domain D1 then you wont be able to read the cookies set by another domain D2. That means you will be able to read cookie "abc" if it is set by the same domain under which you are working.

Upvotes: 0

Mike
Mike

Reputation: 2605

You're only calling GetCookie once before your loop, you want to move it into the checkCookie function to check it on each iteration.

Upvotes: 2

Related Questions