展会信息港展会大全

js文件Cookie存取值的使用
来源:互联网   发布日期:2016-01-28 12:58:45   浏览:2984次  

导读:这篇文章主要介绍了js文件Cookie存取值的使用,需要的朋友可以参考下代码如下: *Cookie工具使用方法: 存值var value = 7天;tools cookie(day,value, {expires:7}); 将字符串:7天 以 day这个key ...

这篇文章主要介绍了js文件Cookie存取值的使用,需要的朋友可以参考下

代码如下:

/*

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;

}

};

赞助本站

人工智能实验室

相关热词: Cookie js

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港