Skip to content

Instantly share code, notes, and snippets.

@maolion
Last active August 29, 2015 14:08
Show Gist options
  • Save maolion/df4bc356036f0637939d to your computer and use it in GitHub Desktop.
Save maolion/df4bc356036f0637939d to your computer and use it in GitHub Desktop.
Cookie
/***
* AKD
* ----------------------------------------------------------------------------
* <Cookie.js> - 2014/8/20
* @version 0.1
* @author Joye, maolion.j@gmail.com
* @website http://maolion.com
*/
function cookie(key, value, expires, path){
if (arguments.length === 1) {
var match = document.cookie.match(new RegExp('(?:^|\\s+)'+key+'=([^;]*);?', 'm'));
return match ? match[1] : undefined;
} else {
document.cookie = key + '=' + value
+ (expires ? ";expires=" + cookie.parseExpires(expires) : '')
+ ";path=" + (path||"/")
;
}
};
cookie.MT = 2678400000;
cookie.WT = 604800000;
cookie.DT = 86400000;
cookie.HT = 3600000;
cookie.parseExpires = function(expires) {
var
e = 0,
d = new Date(),
u = (''+expires).slice(-1).toUpperCase() + 'T'
;
if (cookie.hasOwnProperty(u)) {
e = d.getTime() + ~~expires.slice(0, -1) * cookie[u];
} else if(expires == -1){
e = d.getTime() - 1;
} else {
e = ~~expires;
}
d.setTime(e);
return d.toGMTString();
};
/*
cookie("a","b");
cookie("a", "b", "1w");一周期限 nw 是n周 nm是n月 nd 是 n天 nh是 n小时
cookie("a", "b", "1w", "/"); 最后一个是域
var d = cookie("a);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment