js文件Cookie存取值示例代碼
更新時間:2014年02月20日 16:40:37 作者:
這篇文章主要介紹了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;
}
};
相關文章
disable-devtool禁用web開發(fā)者工具保護網頁源碼
這篇文章主要為大家介紹了disable-devtool禁用web開發(fā)者工具保護網頁源碼的使用,防止源碼泄露保護網站源碼的最佳解決方案,一行代碼就可以搞定,有需要的可以學習參考下2023-11-11
JS實現簡單的選擇題測評系統(tǒng)代碼思路詳解(demo)
本文給大家分享js實現簡單的選擇題測評系統(tǒng)實例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09
Angular+Bootstrap+Spring Boot實現分頁功能實例代碼
這篇文章主要介紹了Angular+Bootstrap+Spring Boot實現分頁功能實例代碼,需要的朋友可以參考下2017-07-07
layer.open提交子頁面的form和layedit文本編輯內容的方法
今天小編就為大家分享一篇layer.open提交子頁面的form和layedit文本編輯內容的方法,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09

