0

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/

That guy
  • 331
  • 1
  • 2
  • 11
  • 1
    As the error console shows, there's no function `getCookie()`. Did you mean `checkCookie()`? – Michael Berkowski Jul 26 '12 at 20:39
  • 2
    I assume `getCookie()` is taken from http://stackoverflow.com/questions/4003823/javascript-getcookie-functions? – Jason McCreary Jul 26 '12 at 20:40
  • Indeed, no such function as `getCookie`. You either have to parse [`document.cookie`](https://developer.mozilla.org/en/DOM/document.cookie) yourself or grab a library that does this for you. – Brad Christie Jul 26 '12 at 20:40

2 Answers2

2

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

Mike
  • 2,547
  • 1
  • 24
  • 36
0

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.

Somnath
  • 3,247
  • 4
  • 28
  • 42
  • I think you could be right. What about if D2 set a cookie on D1 can can D1 read the cookie? – That guy Jul 26 '12 at 21:49
  • Because This is exactly the case where my ad company sets a cookie on my users computer – That guy Jul 26 '12 at 21:51
  • Domain D2 cannot set cookies for domain D1. You can pass the required information to D1 while redirecting to D1 from D2. And let D1 set cookies for itself. Try this link : http://stackoverflow.com/questions/6761415/how-to-set-a-cookie-for-another-domain – Somnath Jul 27 '12 at 06:38