localstorage 本地儲存

2021-09-23 16:04:34 字數 1717 閱讀 7454

define(function()   

try catch (e)

},

encode = encodeuricomponent,

issupportlocalstorage = (function() ;

if (support)

return support;

} catch (e)

}()),

stringify = function(v)

return encode(v);

},

validatecookiename = function(name)

},

// todo: 不支援localstorage時換用cookie儲存

// 現在在某些瀏覽器下可能存在cookie數量的限制

// 之後可能的優化是使用subcookie的方式:

s = issupportlocalstorage ? localstorage :

k = stringify(k);

v = stringify(v);

// 高階瀏覽器中一般合併字元用+比用join('')更高效

// 參考:

doc.cookie = k + '=' + v + '; expires=' + expires.togmtstring();

},

getitem: function(k)

v = decode(cookie.substring(start + k.length, end));

} return v;

},

removeitem: function(k)

},

prefix = '_bm:';

return

s.setitem(prefix + k, json.stringify());

},

get: function(k)

expires = parseint(v.expires, 10);

v = v.value;

if (expires)

this.remove(k);

} return v;

},

remove: function(k, v) catch (e) {}

} else

},

add: function(k, v, options) ,

opts = _.extend(defaults, options),

type = opts.type,

expires = opts.expires,

limit = opts.limit;

// todo: 有潛在的bug, 比如之前儲存的是array型別

if (!_.isarray(h))

if (type === 'set' && _.indexof(h, v) !== -1)

if (_.isnumber(limit) && limit > 0)

h.unshift(v);

this.set(k, h, expires);

} };

});

localStorage 本地儲存

1 localstorage和sessionstorage一樣都是用來儲存客戶端臨時資訊的物件。2 他們均只能儲存字串型別的物件 雖然規範中可以儲存其他原生型別的物件,但是目前為止沒有瀏覽器對其進行實現 3 localstorage生命週期是永久,這意味著除非使用者顯示在瀏覽器提供的ui上清除loc...

本地儲存localStorage

1.本地儲存localstorage概念 window物件下面的屬性,html5新增的,將5m大小的資料儲存本地的瀏覽器上面。2.本地儲存localstorage特點 本地儲存屬於永久儲存,除非手動刪除。html5新增的,高版本的瀏覽器支援。目前所有的瀏覽器都會把localstorage資料型別設定...

本地儲存(localStorage)

1.概念 window物件下面的屬性,html5新增的,將5m大小的資料儲存本地的瀏覽器上面。3.本地儲存localstorage特點 本地儲存屬於永久儲存,除非手動刪除。html5新增的,高版本的瀏覽器支援。目前所有的瀏覽器都會把localstorage資料型別設定字串string.不同的瀏覽器本...