首页 > 其他 > 详细

设置cookie和获取cookie

时间:2016-03-30 22:05:45      阅读:237      评论:0      收藏:0      [点我收藏+]

/**
 * Created by admin on 2016/3/30.
 */

function setCookie(name,value,dayToLive){
    var cookie = name + "=" +encodeURIComponent(value);
    if(typeof dayToLive === "number"){
        cookie += ";max-age="+(dayToLive * 24 * 60 * 60);

    }
    document.cookie = cookie;
}

function getCookies(){
    var cookies = {};
    var all = document.cookie;
    if(all === ""){
        return cookies;
    }

    var list = all.split("; ");
    for(var i=0,len=list.length;i<len;i++){
        var cookie = list[i];
        var p = cookie.indexOf("=");
        var name = cookie.substring(0,p);
        var value = decodeURIComponent(cookie.substring(p+1));
        cookies[name] = value;
    }
    return cookies;

}

setCookie("aaa","111",5);
setCookie("bbb","222",5);

var cookies = getCookies();
for(var i in cookies){

//注意!!!火狐浏览器下会默认多出两个私有属性,但均是以"_"开头
    if(cookies.hasOwnProperty(i) && i.indexOf("_") != 0){
//        document.cookie = i+"=0;expires="+new Date(0).toUTCString();
        alert(i+" : " + cookies[i]);
    }
}

//当单独针对IE写缓存时,可以使用IE userData,IE userData的作用域限制在和当前文档同目录的文档中





 

设置cookie和获取cookie

原文:http://www.cnblogs.com/sdwrz/p/5339065.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!