<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function Cookie(document, name, hours, path, domain, secure) {
            this.$document = document;
            this.$name = name;
            if (hours)
                this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
            else this.$expiration = null;
            if (path) this.$path = path; else this.$path = null;
            if (domain) this.$domain = domain; else this.$domain = null;
            if (secure) this.$secure = true; else this.$secure = false;
        }
        Cookie.prototype.store = function() {
            var cookieval = "";
            for (var prop in this) {
                // Ignore properties with names that begin with '$' and also methods.
                if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
                    continue;
                if (cookieval != "") cookieval += '&';
                cookieval += prop + ':' + escape(this[prop]);
            }
            var cookie = this.$name + '=' + cookieval;
            if (this.$expiration)
                cookie += '; expires=' + this.$expiration.toGMTString();
            if (this.$path) cookie += '; path=' + this.$path;
            if (this.$domain) cookie += '; domain=' + this.$domain;
            if (this.$secure) cookie += '; secure';            // Now store the cookie by setting the magic Document.cookie property.
            this.$document.cookie = cookie;
        }
        Cookie.prototype.load = function() {
            var allcookies = this.$document.cookie;
            if (allcookies == "") return false;            var start = allcookies.indexOf(this.$name + '=');
            if (start == -1) return false;   // Cookie not defined for this page.
            start += this.$name.length + 1;  // Skip name and equals sign.
            var end = allcookies.indexOf(';', start);
            if (end == -1) end = allcookies.length;
            var cookieval = allcookies.substring(start, end);            var a = cookieval.split('&');    // Break it into array of name/value pairs.
            for (var i = 0; i < a.length; i++)  // Break each pair into an array.
                a[i] = a[i].split(':');            for (var i = 0; i < a.length; i++) {
                this[a[i][0]] = unescape(a[i][1]);
            }            return true;
        }        // This function is the remove() method of the Cookie object.
        Cookie.prototype.remove = function() {
            var cookie;
            cookie = this.$name + '=';
            if (this.$path) cookie += '; path=' + this.$path;
            if (this.$domain) cookie += '; domain=' + this.$domain;
            cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';            this.$document.cookie = cookie;
        }        var visitordata = new Cookie(document, "name_color_count_state", 240);        if (!visitordata.load() || !visitordata.name || !visitordata.color) {
            visitordata.name = prompt("What is your name:", "");
            visitordata.color = prompt("What is your favorite color:", "");
        }        if (visitordata.visits == null) visitordata.visits = 0;
        visitordata.visits++;        visitordata.store();        document.write('<font size="7" color="' + visitordata.color + '">' +
               'Welcome, ' + visitordata.name + '!' +
               '</font>' +
               '<p>You have visited ' + visitordata.visits + ' times.');
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" value="Forget My Name" onclick="visitordata.remove();">
    </div>
    </form>
</body>
</html>username=jsh; pwd=jsh; button1=yyy; button2=jjj; button3=jjj; name_color_count_state=name:jsh&color:red&visits:1
下面这个就是他的cookie,与前面的比就是将cookie的值 串成 key:value&key:value的形式。这样做有什么用吗?
这与我直接设置 某一个listproductcookie 的值 为: productlistcookie=name:jsh&pwd:jsh&key:value.这样不是一样吗?
还有这里用“:”会不会产生错误的?不过,我测试过是不会的。
请问这样操作cookie好吗?
还是用jquery 插件好?

解决方案 »

  1.   

    都差不多,能够正确存,读就可以了。
    写成 productlistcookie=name:jsh&pwd:jsh&key:value 
    取的时候适当拆分
      

  2.   

    我觉得我们在读写cookie的时候系统帮我们做了很多东西。。
    而且他这个要先加载然后再存储的
      

  3.   

    这样比较简单 明了 呵呵 
    #    function addCookie(name,value,expireHours){  
    #     var CookieStr=name+"="+escape(value);  
    #     if(expireHours>0){  
    #      var date=new Date();  
    #      date.setTime(date.getTime()+expireHours*3600*1000);  
    #      CookieStr=CookieStr+";expire="+date.toGMTString();  
    #     }  
    #     document.cookie=CookieStr;  
    #    }  
    #    function getCookie(name){  
    #    var strCookie=document.cookie;  
    #    var arrCookie=strCookie.split(";");  
    #    for(var i=0;i<arrCookie.length;i++){  
    #    var arr=arrCookie[i].split("=");  
    #    if(arr[0]==name){  
    #    return arr[1];  
    #    }  
    #    return "";  
    #    }  
    #    }  
      

  4.   


    jquery当然是比较简单些,兼容性也好
      

  5.   

    这要看你的项目是否都用Jquery来做吧,如果只为了一个Cookies来引用一个Jquery,我觉得有点得不偿失。。
      

  6.   

    jq的东西是好,不过,我觉得jquery 的验证插件不好,太死板,有时候满足不了自己的需求。。
    题外话啦。。
    谢谢大家
      

  7.   

    用JS操作cookie 是不是比后台操作cookie要好很多?
      

  8.   

    楼主的先拼接,然后在拆分。。
    我觉得用jQuery方便多了..而且兼容要很多的。。
      

  9.   

    ":"如果不处理应该会产生错误 吧
    都可有优点吧,cookie本来长度都有限制,加在一起,更容易超出长度限制了
    另一方面cookie 中key的个数也有限制,这样又有好处
    more detail
      

  10.   

    好,谢谢大家。。
    sky 有给了我另一个问题的答案啦。。谢谢