js檔案Cookie存取值示例程式碼

NO IMAGE

複製程式碼 程式碼如下:
/*
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;
}
};

您可能感興趣的文章:

JS基於cookie實現來賓統計記錄訪客資訊的方法js cookies實現懸浮購物車的方法用js讀、寫、刪除Cookie程式碼續篇js讀取cookie方法總結js 通過cookie實現重新整理不變化樹形選單JS實現清除指定cookies的方法js設定cookie過期當前時間減去一秒相當於立即過期用js讀、寫、刪除Cookie程式碼分享及詳細註釋說明什麼是cookie?js手動建立和儲存cookieJS使用cookie實現DIV提示框只顯示一次的方法