<html>
<head>
<script type= "text/javascript" src="http://code.hs-cn.com/jquery/jquery-1.7.1.min.js" ></script>
<script type= "text/javascript" >
localData = {
hname: location.hostname ? location.hostname : 'localStatus',
isLocalStorage: window.localStorage ? true : false,
dataDom: null,
initDom : function () { //初始化userData
if (!this.dataDom) {
try {
this. dataDom = document. createElement( 'input'); //这里使用hidden的input元素
this. dataDom. type = 'hidden';
this. dataDom. style. display = "none";
this. dataDom. addBehavior( '#default#userData'); //这是userData的语法
document. body. appendChild( this. dataDom);
var exDate = new Date();
exDate = exDate. getDate() + 30;
this. dataDom. expires = exDate. toUTCString(); //设定过期时间
} catch (ex) {
return false;
}
}
return true;
},
set: function (key, value) {
if (this.isLocalStorage) {
window.localStorage.setItem(key, value);
} else {
if (this.initDom()) {
this. dataDom. load( this. hname);
this. dataDom. setAttribute(key, value);
this. dataDom. save( this. hname)
}
}
},
get: function (key) {
if (this.isLocalStorage) {
return window .localStorage .getItem (key);
} else {
if (this.initDom()) {
this. dataDom. load( this. hname);
return this. dataDom. getAttribute(key);
}
}
},
remove : function (key) {
if (this.isLocalStorage) {
localStorage.removeItem(key);
} else {
if (this.initDom()) {
this. dataDom. load( this. hname);
this. dataDom. removeAttribute(key);
this. dataDom. save( this. hname)
}
}
}
}
function sart() {
localData .initDom ();
localData .set ("shikun" ,"[{'username':'雄伟','id':'1'},{'username':'雄伟1','id':'2'},{'username':'雄伟3','id':'3'}]" );
var tJson = eval('(' + localData.get ("shikun" )+ ')');
for (var i = 0; i< tJson. length; i++){
console.log (tJson [i ]["username" ]);
}
}
window.onload = sart();
// parent.location.reload();
</script>
</head>
html5本地存数据
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 一、本地存储由来的背景 由于HTML4时代Cookie的大小、格式、存储数据格式等限制,网站应用如果想在浏览器端存...
- sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,...
- 引用文档: HTML5本地存储——IndexedDB(一:基本使用)[http://www.cnblogs.com...