简体   繁体   English

为什么这个javascript不起作用?

[英]Why wont this javascript work?

I'm trying to detect the existence of a specific cookie "abc". 我正在尝试检测特定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. 这段代码唯一的问题是它没有检测到cookie的存在。 Even when I change the cookie name to one that already exists.You can check it out here http://jsfiddle.net/aMZj3/ 即使我将cookie名称更改为已存在的名称。您可以在此查看http://jsfiddle.net/aMZj3/

你只是在循环之前调用一次GetCookie,你想将它移动到checkCookie函数中以在每次迭代时检查它。

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. 请注意:据我所知,如果您在域D1下工作,那么您将无法读取由另一个域D2设置的cookie。 That means you will be able to read cookie "abc" if it is set by the same domain under which you are working. 这意味着如果cookie由您工作的同一域设置,您将能够读取cookie“abc”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM