设置Cookie时要把一个json整体放入Cookie而不是单独放入,影响效率。
var Cookies = {};
/**
* 设置Cookies
*/
var Cookies = {};
/**
* 设置Cookies
*/
Cookies.set = function(name, value,hours){
var argv = arguments;
var argc = arguments.length;
var ms = hours * 3600 * 1000;
date.setTime(date.getTime() + ms);
var expires=date.toGMTString();
var path = (argc > 3) ? argv[3] : ‘/‘;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + exp.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
};
/**
* 读取Cookies
*/
Cookies.get = function(name){
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
var j = 0;
while(i < clen){
j = i + alen;
if (document.cookie.substring(i, j) == arg)
return Cookies.getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if(i == 0)
break;
}
return null;
};
/**
* 清除Cookies
*/
Cookies.clear = function(name) {
if(Cookies.get(name)){
var expdate = new Date();
expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
Cookies.set(name, "", expdate);
}
};
Cookies.getCookieVal = function(offset){
var endstr = document.cookie.indexOf(";", offset);
if(endstr == -1){
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
};
把json转为字符串:
var data = {
‘vipflag‘ : ‘1‘,
‘usertype‘ : ‘11‘,
‘uid‘ : ‘12345‘,
‘token‘ : ‘654‘,
‘thumb‘ : ‘687.jpg‘,
‘startTime‘ : ‘‘,
‘preTime‘ : ‘‘,
‘nickName‘ : ‘%u68EE%u6797%u91CE%u5C0F%u5B50‘,
‘endTime‘ : ‘‘,
‘date‘ : ‘20150625152139‘,
‘channelid‘ : ‘100218‘,
‘auctionTime‘ : ‘‘,
‘activityId‘ : ‘‘
};
var obj =JSON.stringify(data);
原文:http://www.cnblogs.com/share123/p/4615748.html