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;
}
};
相關文章
javascript中加var和不加var的區(qū)別 你真的懂嗎
var 語句用于聲明變量,本文給大家介紹javascript 中加’var‘和不加'var'的區(qū)別,涉及到javascript var相關知識,對javascript var相關知識感興趣的朋友一起學習吧2016-01-01JS+DIV+CSS實現(xiàn)的經(jīng)典標簽切換效果代碼
這篇文章主要介紹了JS+DIV+CSS實現(xiàn)的經(jīng)典標簽切換效果代碼,涉及JavaScript基于鼠標事件針對頁面元素動態(tài)變換的實現(xiàn)技巧,頁面美觀實用,需要的朋友可以參考下2015-09-09關于js二維數(shù)組和多維數(shù)組的定義聲明(詳解)
下面小編就為大家?guī)硪黄P于js二維數(shù)組和多維數(shù)組的定義聲明(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10