码迷,mamicode.com
首页 > 其他好文 > 详细

cookie的处理

时间:2018-10-12 13:55:08      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:undefined   str   ons   encode   argument   pat   string   location   raw   

window.gg = {}
(function () {
function setCookie(key, value, options) {
if (!(getCookie(key) == "")) {
delCookie(key);
}
options = options || {};
if (value === null || value === undefined) {
options.expires = -1;
} else {
if (options.expires == null) {
options.expires = new Date();
options.expires.setFullYear(options.expires.getFullYear() + 1);
}
}

if (typeof options.expires === ‘number‘) {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
value = String(value);
document.cookie =
[
encodeURIComponent(key), ‘=‘, options.raw ? value : encodeURIComponent(value),
//options.expires ? ‘; expires=‘ + options.expires.toUTCString() : ‘‘,
‘; expires=‘ + options.expires.toUTCString(),
options.path ? ‘; path=‘ + options.path : ‘;path=/‘,
options.domain ? ‘; domain=‘ + options.domain : ‘‘,
options.secure ? ‘; secure‘ : ‘‘
].join(‘‘);
}

function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr == null && name == "LxLoginRoleId") {
window.location = "index.html";
}
return arr == null ? null : decodeURIComponent(arr[2]);
}

function delCookie(name) {
var exp = new Date();
exp.setFullYear(exp.getFullYear() - 1);
var cval = getCookie(name);
var delCookie = name + "=" + cval + ";expires=" + exp.toUTCString() + "; path=/";
if (cval != null) document.cookie = delCookie;
}

gg.core.cookie = {
setValue: function (key, value, options) {
if (arguments.length > 1 && String(value) !== "[object Object]") {
setCookie(key, value, options);
return;
}
},
getValue: function (key) {
return getCookie(key);
},
deleteCookid: function (key) {
delCookie(key);
return;
}
};
})();

cookie的处理

标签:undefined   str   ons   encode   argument   pat   string   location   raw   

原文地址:https://www.cnblogs.com/tuhazi/p/9777259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!