欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

javascript原生cookie之jscookie.js

程序员文章站 2024-02-16 18:54:28
...
javascript实现cookie操作,独立的javascript插件;
使用方法请阅读:https://git.oschina.net/wuquanyao/JsCookie
/*+==============================================
+ 我不只是一个程序员,我更希望用此创造价值
+ author:wuquanyao
+ email:wqynqa@163.com
* version:1.0.2
+==============================================*/
var cookies = (function(){
var Cookies;
Cookies = {
set:function(key,value,day,path){
day = day || 0.5;
path = path || "/";
document.cookie = key+"="+escape(value)+";expires="+expire(day)+";path="+path;
},
get:function(key){
return getCookies(key);
},
remove:function(key){
document.cookie = key+"="+getCookies(key)+";expires="+expire(-1);
},
clear:function(){
clearCookies();
},
has:function(key){
var _cookies = allCookies(), r = false;
for(var i in _cookies){
if(trim(_cookies[i][0]) === key){
r = true;
break;
}
}
return r;
},
stringify:function(data){
return JSON.stringify(data);
},
parse:function(data){
return JSON.parse(data);
},
trim:function(string){
return trim(string);
},
dump:function(data){
console.log(data);
}
}
function expire(day){
var exp = new Date();
exp.setTime(exp.getTime()+day*24*3600*1000);
return exp.toUTCString();
}
function allCookies(){
var _cookies;
_cookies = document.cookie;
_cookies = _cookies.split(';');
for(var i in _cookies){
_cookies[i] = _cookies[i].split('=');
}
return _cookies;
}
function getCookies(key){
var _cookies = allCookies(), o={};
for(var i in _cookies){
o[trim(_cookies[i][0])] = _cookies[i][1];
}
return unescape(o[key]);
}
function clearCookies(){
var _cookies = allCookies();
for(var i in _cookies){
document.cookie = _cookies[i][0]+"="+unescape(_cookies[i][1])+";expires="+expire(-1);
}
}
function trim(string){
return string.replace(/(^\s*)|(\s*$)/,'');
}
return Cookies;
})();

javascript原生cookie之jscookie.js QQ截图20151027102037.png ( 123.58 KB 下载:13 次 )

AD:真正免费,域名+虚机+企业邮箱=0元