HTML5本地存储localStorage、sessionStorage及IE专属UserData 在客户端存储数据用的最普遍的方式非cookie莫属,随着HTML5的普及,新的本地存储方式localStorage和sessionStorage将在标准浏览器上大展身手。l ...
HTML5本地存储localStorage、sessionStorage及IE专属UserData
<script type="text/javascript"> var UserData = { userData: null, name: document.location.hostname, init: function() { if (!this.userData) { try { this.userData = document.createElement("INPUT"); this.userData.type = "hidden"; this.userData.style.display = "none"; this.userData.addBehavior("#default#userData"); document.body.appendChild(this.userData); var expires = new Date(); expires.setDate(expires.getDate() + 365); this.userData.expires = expires.toUTCString(); } catch (e) { return false; } } return true; }, setItem: function(key, value) { if (this.init()) { this.userData.load(this.name); this.userData.setAttribute(key, value); this.userData.save(this.name); } }, getItem: function(key) { if (this.init()) { this.userData.load(this.name); return this.userData.getAttribute(key) } }, remove: function(key) { if (this.init()) { this.userData.load(this.name); this.userData.removeAttribute(key); this.userData.save(this.name); } } };</script>
总结:在IE6、IE7市场份额逐渐下降的情况下,localStorage和sessionStorage将会应用的越来越广泛,cookie除了在某些必要的情况下,完全可以用localStorage替代,当然,不建议在localStorage里面保存敏感数据,因为localStorage可以随时读取修改或删除。
参考文档:http://www.css88.com/archives/3717
By:王美建 from:http://www.cnblogs.com/wangmeijian/p/4518606.html 转载请保留署名和出处!
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:HTML5本地存储localStorage、sessionStorage及IE专属UserData
关键词:HTML
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。