本地cookie的读、写、删除

本文深入探讨了如何使用 JavaScript 来操作 cookie,包括读取、设置和删除 cookie 的方法。
    <script type="text/javascript"> 
        //操作cookie,提供读、写、删除
        //document.cookie = "cookieName=cookieValue; expirationdata=timeValue; path=pathValue";
        var GLOBAL = {};
        GLOBAL.cookie = {
            //读取
            read : function(name) {
                var cookieStr = "; " + document.cookie + "; ";
                var index = cookieStr.indexOf("; " + name + "=");
                if (-1 != index) {
                    var s = cookieStr.substring(index + name.length + 3);
                    return unescape(s.split("; ")[0]);
                    //return unescape(s.substring(0, s.indexOf("; ")));
                } else {
                    return null;
                }
            },
            //设置
            //@name     cookie键
            //@value    cookie值
            //@expires  过期天数
            set : function(name, value, expires) {
                var expDays = expires * 24 * 60 * 60 *1000;
                var expDate = new Date();
                expDate.setTime(expDate.getTime() + expDays);
                var expString = expires ? "; expires=" + expDate.toGMTString() : "";
                var pathString = "; path=/";
                document.cookie = name + "=" + escape(value) + expString + pathString;
            },
            //删除
            del : function(name) {
                var exp = new Date(new Date().getTime() - 1);
                var s = this.read(name);
                if (null != s) {
                    document.cookie = name + "=" + s + "; expires=" + exp.toGMTString() + "; path=/";
                }
            }
        };
    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值