var script=document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "http://code.jquery.com/jquery-1.8.3.min.js");
var heads = document.getElementsByTagName("head");
if(heads.length) heads[0].appendChild(script);
else document.documentElement.appendChild(script);
script.onload = function(){
(function(){
$.extend({
isJson : function(str) {
if ($.type(str)== 'string') {
try {
var obj=JSON.parse(str);
if($.type(obj) == 'object' && obj ){
return true;
} else {
return false;
}
} catch(e) {
//console.log('error:'+str+'!!!'+e);
return false;
}
}
return false;
},
storage : function(name, value, expire){
// expire 參數(shù) 格式 {expire : 1} 注:expire.expire 的值 等于過(guò)期時(shí)間減去當(dāng)前時(shí)間, 一個(gè)大于0的值,單位毫秒。
//判斷全局是否過(guò)期
var expires = JSON.parse(localStorage.getItem('expire')) || {'expire': 0};
if(expires.expire && expires.expire < new Date().getTime()) this.removeStorage();
//沒(méi)有參數(shù)時(shí),返回原生 localStorage
if(arguments.length == 0) return localStorage;
if(arguments[0] == null || arguments[0] == "") return;
if(name && name in expires && expires[name] && expires[name] < new Date().getTime()){
this.removeStorage(name);
delete expires[name];
localStorage.setItem('expire', JSON.stringify(expires));
}
if(arguments.length == 1){
if($.type(arguments[0]) == 'string'){
//只有一個(gè)字符串參數(shù) 'expire' 時(shí) 返回全局的過(guò)期時(shí)間
if(arguments[0] == 'expire' || arguments[0] == ':') return expires.expire;
//只有一個(gè)字符串參數(shù) 'expires' 時(shí) 返回json數(shù)據(jù) 所有的過(guò)期時(shí)間 包含全局
if(arguments[0] == 'expires' || arguments[0] == '::') return expires;
//只有一個(gè)字符串參數(shù) 以冒號(hào)或雙冒號(hào)開頭的字符串 冒號(hào)后面是要查找到 [name] 返回[name]的過(guò)期時(shí)間
if(/^:{1,2}[\w-]+$/.test(arguments[0])){
name = arguments[0].replace(/^:{1,2}([\w-]+)$/);
if(name in expires && expires.name) return expires[name];
return 0;
}
//只有一個(gè)字符串參數(shù)時(shí),返回當(dāng)前的值
var ret = localStorage.getItem(name)
if(this.isJson(ret)) ret = JSON.parse(ret);
return ret;
}
//只有一個(gè) json對(duì)象參數(shù)并且它包含expire屬性
if($.type(arguments[0]) == 'object' && 'expire' in arguments[0] && arguments[0].expire){
//值為字符串時(shí) 字符串是要查找到 [name] 返回[name]的過(guò)期時(shí)間
if($.type(arguments[0].expire) == 'string'){
name = arguments[0].expire;
if(name in expires && expires.name) return expires[name];
return 0;
}
//值為數(shù)字時(shí) 設(shè)置全局過(guò)期時(shí)間
if($.type(arguments[0].expire) == 'number'){
expires.expire = new Date().getTime() + arguments[0].expire;
localStorage.setItem('expire', JSON.stringify(expires));
}
}
}
//有2個(gè)參數(shù) 第1個(gè)是字符串,第2個(gè)是 expire對(duì)象時(shí)
if(arguments.length == 2 && $.type(arguments[0]) == 'string' && arguments[0] != '' && $.type(arguments[1]) == 'object' && Object.keys(arguments[1]).length == 1 && 'expire' in arguments[1]) expire = arguments[1].expire;
//保存當(dāng)前 expire
if(expire && $.type(expire) == 'object'){
expires[name] = new Date().getTime() + expire.expire;
localStorage.setItem('expire', JSON.stringify(expires))
}
if(value == null || value == "") return this;
if($.type(value) == 'object' || $.type(value) == 'array') value = JSON.stringify(arguments[1]);
localStorage.setItem(name, value);
return this;
},
removeStorage : function(name){
if(name == null) localStorage.clear();
else localStorage.removeItem(name);
return this;
},
session : function(name, value){
if(name == null) return sessionStorage; //沒(méi)有參數(shù)時(shí),返回原生 sessionStorage
if(value == null){
var ret = sessionStorage.getItem(name);
if(this.isJson(ret)) ret = JSON.parse(ret);
return ret;
}
if($.type(value) == 'object' || $.type(value) == 'array') value = JSON.stringify(value);
sessionStorage.setItem(name, value);
return this;
},
removeSession : function(name){
if(name == null) sessionStorage.clear();
else sessionStorage.removeItem(name);
return this;
},
cookie : function(name, value, options){
var cookie = document.cookie.replace(' ', '');
//當(dāng)沒(méi)有參數(shù)時(shí) 返回原生 cookie 對(duì)象
if(arguments.length == 0) return unescape(cookie);
if(name == null || $.type(name) != 'string' || name == '') return;
//當(dāng)只有一個(gè)參數(shù)時(shí) 返回 value 值, 當(dāng)?shù)诙?shù)為布爾型時(shí) true : 返回 json 所有數(shù)據(jù)。 false : 與一個(gè)參數(shù)時(shí)相同。
if(arguments.length == 1 || (arguments.length == 2 && $.type(arguments[1]) == 'boolean')){
var json = {};
$.each(cookie.split(';'), function(i,str){
var arr = str.split('=');
json[arr[0]] = unescape(arr[1]);
});
if(arguments.length == 2 && arguments[1]) return json;
//讀取 name 值
var ret = unescape(json[name]);
//如果是JSON字符串,轉(zhuǎn)成JSON對(duì)象
if(this.isJson(ret)) ret = JSON.parse(ret);
return ret;
}
//以下是寫入cookie
var str = ';';
// json { domain : '', path : '', expires : 0, secure : '' } expire 或 expires 都可以 單位豪秒
// expire 或 expires 參數(shù) 格式 {expire : 1} 注:expire.expire 的值 等于過(guò)期時(shí)間減去當(dāng)前時(shí)間, 一個(gè)大于0的值,單位毫秒。
if(options || $.type(options) == 'object'){
//把 expire 屬性 修正為 expires
if('expire' in options && options.expire && $.type(options.expire) == 'number'){
options.expires = options.expire;
delete options.expire;
}
//處理過(guò)期時(shí)間,單位毫秒
if('expires' in options && options.expires && $.type(options.expires) == 'number'){
var exp = new Date();
exp.setTime(exp.getTime() + options.expires);
options.expires = exp.toGMTString();
}
for(var key in options){
str += key + '=' + options[key] + ';';
}
}
document.cookie = name + "=" + escape(value) + str;
return this;
},
removeCookie : function(name, options){
return this.cookie(name, '', $.extend(options, {expires: -1}));
}
});
})(jQuery);
$.storage('a','localStorage');
console.log($.storage('a'))
$.removeStorage('a');
console.log($.storage('a'));
$.session('b','sessionStorage');
console.log($.session('b'))
$.removeSession('b');
console.log($.session('b'));
$.cookie('c','cookie');
console.log($.cookie('c'))
$.removeCookie('c');
console.log($.cookie('c'));
舉報(bào) jquery 封裝 localStorage, sessionStorage, cookie. localStorage 支持過(guò)期設(shè)置
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
- 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 存儲(chǔ)類型 他們均只能存儲(chǔ)字符串類型的對(duì)象(雖然規(guī)范中可以存儲(chǔ)其他原生類型的對(duì)象,但是目前為止沒(méi)有瀏覽器對(duì)其進(jìn)行實(shí)現(xiàn)...
- sessionStorage 、localStorage 和 cookie 之間的區(qū)別 共同點(diǎn):都是保存在瀏覽器端...
- Cookie LocalStorage SessionStorage對(duì)比 三者異同 操作 localStora...
- 首先,從存儲(chǔ)位置來(lái)看,可以分為服務(wù)端存儲(chǔ)和客戶端存儲(chǔ)兩種。 服務(wù)端存儲(chǔ):session瀏覽器端存儲(chǔ):cookie,...