V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yqyq1020
V2EX  ›  问与答

javascript权威指南里面的一个cookie工具类实例,运行之后不太好,什么原因?

  •  
  •   yqyq1020 · 2012-12-21 20:36:15 +08:00 · 2952 次点击
    这是一个创建于 4158 天前的主题,其中的信息可能已经有所发展或是发生改变。
    function Cookie(name){
    this.$name = name;
    var allcookies = document.cookie;
    if (allcookies == "") return;
    var cookies = allcookies.split(';');
    var cookie = null;
    for(var i=0;i<cookies.length;i++){
    if (cookies[i].substring(0,name.length+1) == (name + "=")){
    cookie = cookies[i];
    break;
    }
    }
    if (cookie == null) return;
    var cookieval = cookie.substring(name.length+1);
    var a = cookieval.split('&');
    for(var i=0;i<a.length;i++);
    a[i]=a[i].split(':');
    for(var i=0;i<a.length;i++){
    this[a[i][0]] = decodeURIComponent(a[i][1]);
    }
    }
    Cookie.prototype.store = function(daysToLive,path,domain,secure){
    var cookieval = "";
    for(var prop in this){
    if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
    continue;
    if(cookieval !="")cookieval += '&';
    cookieval += prop + ':' + encodeURIComponent(this[prop]);
    }
    var cookie = this.$name+'='+cookieval;
    if(daysToLive || daysToLive == 0){
    cookie += ";max-age" + (daysToLive*24*60*60);
    }
    if(path) cookie += ";path="+ path;
    if(domain) cookie+= ";domain=" + domain;
    if(secure) cookie+=";secure";
    document.cookie=cookie;
    }
    Cookie.prototype.remove = function(path,domain,secure){
    for(var prop in this){
    if (prop.charAt(0) != '$' && typeof this[prop] != 'function')
    delete this[prop];
    }
    this.store(0,path,domain,secure);
    }
    Cookie.enabled = function(){
    if(navigator.cookieEnabled !=undefined) return navigaror.cookieEnabled;
    if(Cookie.enabled.cache !=undefined) return Cookie.enabled.cache;
    document.cookie="testcookie=test;max-age=1000";
    var cookies=document.cookie;
    if(cookies.indexOf("testcookie=test") == -1){
    return Cookie.enabled.cache = false;
    }
    else{
    document.cookie="testcookie=test;max-age=0";
    return Cookie.enabled.cache = true;
    }
    }
    /*下面是一个使用该cookie工具类的实例*/
    var cookie=new Cookie("vistordata");
    if(!cookie.name || !cookie.color){
    cookie.name=prompt("What is your name:","");
    cookie.color=prompt("What is your favorite color:","");
    }
    if(!cookie.visits)cookie.visits=1;
    else cookie.visits++;
    cookie.store(10);
    document.write('<h1 style="color:'+cookie.color+'">'+
    'Welcome,'+cookie.name+'!'+'</h1>'+
    '<p>You have visited '+cookie.visits+'times.'+
    '<button onclick="window.cookie.remove();">Forget me</button>');

    上面的实例在chrome里运行后,刷新一次就要求你重新输入信息,不能直接读取cookie显示出来,什么原因求解答。。。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4042 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:13 · PVG 13:13 · LAX 22:13 · JFK 01:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.