码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript操作cookie

时间:2015-11-01 18:08:31      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

设置cookie:

function setCookie(name,value,days,path){
        var t = new Date(),
                  sCookie= name + "=" + escape(value);
                  t.setTime(t.getTime()+days*24*3600*1000);
                 sCookie += ";expires="+t.toGMTString();
        if(path){
                sCookie += ";path="+path;
        }
        document.cookie = sCookie;
}

查询cookie:

function getCookie(name){
        var preg = new RegExp("(^| )"+name+"=([^;]*)(;|$)","g");
        if(preg.test(document.cookie)){
                return RegExp.$2;
        }else{
                return "";
        }
}

删除cookie:

function delCookie(name){
        setCookie(name,"",-1);
}

清空cookie:

function clearCookie(){
        var cookieArr = document.cookie.split(";");
        for(var i=0;i<cookieArr.length;i++){
                var preg = /([^;]*)=([^;]*)/;
                var match = preg.exec(document.cookie);
                delCookie(match[1]);
        }
}


Javascript操作cookie

标签:

原文地址:http://my.oschina.net/yongqingfan/blog/524599

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!