加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

js文件Cookie存取值范例代码

发布时间:2023-10-17 15:04:51 所属栏目:教程 来源:
导读:代码如下:

/*

Cookie工具

使用方法:

//存值

var value = "7天";

tools.cookie("day",value, {expires:7}); //将字符串:"7天" 以 "day"这个key保存到cookie中5天

//取值

var v = too
代码如下:
 
/*
 
Cookie工具
 
使用方法:
 
//存值
 
var value = "7天";
 
tools.cookie("day",value, {expires:7}); //将字符串:"7天" 以 "day"这个key保存到cookie中5天
 
//取值
 
var v = tools.cookie("day"); //用 "day" 这个key从cookie取出值
 
*/
 
tools.cookie = function(name, value, options) {
 
if (typeof value != 'undefined') { // name and value given, set cookie
 
options = options || {};
 
if (value === null) {
 
value = '';
 
options.expires = -1;
 
}
 
var expires = '';
 
if (options.expires
 
&& (typeof options.expires == 'number' || options.expires.toGMTString)) {
 
var date;
 
if (typeof options.expires == 'number') {
 
date = new Date();
 
date.setTime(date.getTime()
 
+ (options.expires * 24 * 60 * 60 * 1000));
 
} else {
 
date = options.expires;
 
}
 
expires = '; expires=' + date.toGMTString(); // use expires
 
// attribute,
 
// max-age is not
 
// supported by IE
 
}
 
var path = options.path ? '; path=' + options.path : '';
 
var domain = options.domain ? '; domain=' + options.domain : '';
 
var secure = options.secure ? '; secure' : '';
 
document.cookie = [ name, '=', encodeURIComponent(value), expires,
 
path, domain, secure ].join('');
 
} else { // only name given, get cookie
 
var cookieValue = null;
 
if (document.cookie && document.cookie != '') {
 
var cookies = document.cookie.split(';');
 
for ( var i = 0; i < cookies.length; i++) {
 
var cookie = jQuery.trim(cookies[i]);
 
// Does this cookie string begin with the name we want?
 
if (cookie.substring(0, name.length + 1) == (name + '=')) {
 
cookieValue = decodeURIComponent(cookie
 
.substring(name.length + 1));
 
break;
 
}
 
}
 
}
 
return cookieValue;
 
}
 
};
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章