javascript实现cookie操作,独立的javascript插件;
使用方法请阅读:https://git.oschina.net/wuquanyao/JsCookie/*+==============================================<br>
+ 我不只是一个程序员,我更希望用此创造价值<br>
+ author:wuquanyao<br>
+ email:wqynqa@163.com<br>
* version:1.0.2<br>
+==============================================*/<br>
var cookies = (function(){<br>
var Cookies;<br>
Cookies = {<br>
set:function(key,value,day,path){<br>
day = day || 0.5;<br>
path = path || "/";<br>
document.cookie = key+"="+escape(value)+";expires="+expire(day)+";path="+path;<br>
},<br>
get:function(key){<br>
return getCookies(key);<br>
},<br>
remove:function(key){<br>
document.cookie = key+"="+getCookies(key)+";expires="+expire(-1);<br>
},<br>
clear:function(){<br>
clearCookies();<br>
},<br>
has:function(key){<br>
var _cookies = allCookies(), r = false;<br>
for(var i in _cookies){<br>
if(trim(_cookies[i][0]) === key){<br>
r = true;<br>
break;<br>
}<br>
}<br>
return r;<br>
},<br>
stringify:function(data){<br>
return JSON.stringify(data);<br>
},<br>
parse:function(data){<br>
return JSON.parse(data);<br>
},<br>
trim:function(string){<br>
return trim(string);<br>
},<br>
dump:function(data){<br>
console.log(data);<br>
}<br>
}<br>
function expire(day){<br>
var exp = new Date();<br>
exp.setTime(exp.getTime()+day*24*3600*1000);<br>
return exp.toUTCString();<br>
}<br>
function allCookies(){<br>
var _cookies;<br>
_cookies = document.cookie;<br>
_cookies = _cookies.split(';');<br>
for(var i in _cookies){<br>
_cookies[i] = _cookies[i].split('=');<br>
}<br>
return _cookies;<br>
}<br>
function getCookies(key){<br>
var _cookies = allCookies(), o={};<br>
for(var i in _cookies){<br>
o[trim(_cookies[i][0])] = _cookies[i][1];<br>
}<br>
return unescape(o[key]);<br>
}<br>
function clearCookies(){<br>
var _cookies = allCookies();<br>
for(var i in _cookies){<br>
document.cookie = _cookies[i][0]+"="+unescape(_cookies[i][1])+";expires="+expire(-1);<br>
}<br>
}<br>
function trim(string){<br>
return string.replace(/(^\s*)|(\s*$)/,'');<br>
}<br>
return Cookies;<br>
})();
QQ截图20151027102037.png ( 123.58 KB 下载:13 次 )
AD:真正免费,域名+虚机+企业邮箱=0元