繁体   English   中英

Javascript Cookie问题IE

[英]Javascript Cookie problems IE

我一直在用一些Javascript做事,请帮忙,我看不到为什么它根本无法在IE 7或8中找到我的cookie

我正在通过另一个事件将cookie设置为true,但是我只想看到IE拾取我最初设置的cookie。 在Firefox中也可以使用,谢谢。

var t=setTimeout("doAlert()",8000);
var doAlertVar = true;
document.cookie =  "closed=0;expires=0;path=";

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie;
    alert(ca);
    ca = ca.replace(/^\s*|\s*$/g,'');
    ca = document.cookie.split(';');

    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function doAlert(){

    if(readCookie('closed')==1){
        doAlertVar = false;
    }
    if(readCookie('closed')==0){
        alert("unlicensed demo version\nbuy online at");

    }
    t=setTimeout("doAlert()",5000);

}

从哪里开始。

setTimeout("doAlert()",8000);
// do not use strings as an argument to setTimeout, that runs eval under the hood.
// use
setTimeout(doAlert,8000);
// instead

document.cookie =  "closed=0;expires=0;path=";
// this is wrong, expires should follow the format Fri, 14 May 2010 17:22:33 GMT (new Date().toUTCString())
// path should be path=/

您也可以使用正则表达式执行此操作:

function readCookie(name, defaultValue) {
  var value = defaultValue;
  document.cookie.replace(new RegExp("\\b" + name + "=([^;]*)"), function(_, v) {
    value = v;
  });
  return value;
}

暂无
暂无

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

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