zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

localstorage 本地存储

存储 本地 localStorage
2023-09-11 14:17:33 时间
decode = function(s) { // 参考jquery cookie的实现: https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js if (s.indexOf(") === 0) { s = s.slice(1, - 1).replace(/\\"/g, ").replace(/\\\\/g, \\); try { return decodeURIComponent(s); } catch (e) { return null; encode = encodeURIComponent, isSupportLocalStorage = (function() { try { var support = localStorage in win win[localStorage] !== null, test = { k: test key, v: test value if (support) { localStorage.setItem(test.k, test.v); support = test.v === localStorage.getItem(test.k); return support; } catch (e) { return false; }()), stringify = function(v) { if (!_.isString(v)) { v = JSON.stringify(v); return encode(v); validateCookieName = function(name) { if (!_.isString(name) || name === ) { throw new TypeError(Cookie name must be a non-empty string!); // TODO: 不支持localStorage时换用cookie存储 // 现在在某些浏览器下可能存在cookie数量的限制 // 之后可能的优化是使用subcookie的方式: https://developer.yahoo.com/yui/cookie/#subcookies s = isSupportLocalStorage ? localStorage : { setItem: function(k, v, days) { validateCookieName(k); // 默认cookie中的结果缓存7天 days = days || 7; var expires = new Date(); expires.setDate(expires.getDate() + days); v = _m.utils.evaluate(v); if (_.isArray(v)) { v = v[0]; expires = new Date(parseInt(v[1], 10)); k = stringify(k); v = stringify(v); // 高端浏览器中一般合并字符用+比用join()更高效 // 参考: http://photo.weibo.com/2785671884/wbphotos/large/photo_id/3453950944633013?refer=weibofeedv5 doc.cookie = k + = + v + ; expires= + expires.toGMTString(); getItem: function(k) { validateCookieName(k); k = stringify(k) + =; var v = null, cookie = doc.cookie, start = cookie.indexOf(k); if (start -1) { var end = cookie.indexOf(;, start); if (end === -1) { end = cookie.length; v = decode(cookie.substring(start + k.length, end)); return v; removeItem: function(k) { this.setItem(k, , - 1); prefix = _BM:; return { isSupportLocalStorage: isSupportLocalStorage, set: function(k, v, expires) { if (_.isNumber(expires)) { expires = +new Date() + expires; s.setItem(prefix + k, JSON.stringify({ value: v, expires: expires })); get: function(k) { var v = JSON.parse(s.getItem(prefix + k)), expires; if (!v) { return; expires = parseInt(v.expires, 10); v = v.value; if (expires) { if (+new Date() expires) { return v; this.remove(k); return v; remove: function(k, v) { if (v) { try { this.set(k, _.difference(this.get(k), v)); } catch (e) {} } else { s.removeItem(prefix + k); add: function(k, v, options) { var h = this.get(k), defaults = { type: set, limit: 10 opts = _.extend(defaults, options), type = opts.type, expires = opts.expires, limit = opts.limit; // TODO: 有潜在的bug, 比如之前存储的是array类型 if (!_.isArray(h)) { h = _.isUndefined(h) [] || [h]; if (type === set _.indexOf(h, v) !== -1) { return; if (_.isNumber(limit) limit 0) { h = _.first(h, limit - 1); h.unshift(v); this.set(k, h, expires);
原生JavaScript实现本地存储(localStorage)和会话存储(sessionStorage) 本地存储和会话存储的目的,是将所输入的文件存入到浏览器中,在需要的时候,直接调用,但是本地存储和会话存储有着一定的区别,本地存储,在不主动删除的情况下,会一直保留在浏览器中,会话存储是,当前保存的内容,页面刷新还会保留在浏览器中,但是关闭页面在打开时,会发现没有数据了。这是他们两的用处和区别。
localStorage灵魂五问。 5M?? 10M !!! localStorage 存储的键和值始终采用 UTF-16 DOMString 格式,每个字符使用两个字节。与对象一样,整数键将自动转换为字符串。
解决浏览器存储问题,不得不了解的cookie,localStorage和sessionStorage 要说浏览器存储的方式,我们听到最多的莫过于就是 cookie , localStorage 和 sessionStorage 了。那这三者之前有什么区别, cookie 与 session 、 sessionId 又有什么关系呢?接下来我们一起来了解一下吧!