<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(); //設(shè)定過期時(shí)間
} 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本地存數(shù)據(jù)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
推薦閱讀更多精彩內(nèi)容
- 一、本地存儲(chǔ)由來的背景 由于HTML4時(shí)代Cookie的大小、格式、存儲(chǔ)數(shù)據(jù)格式等限制,網(wǎng)站應(yīng)用如果想在瀏覽器端存...
- sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,...
- Html5工作上用了有段時(shí)間了,但一直沒有系統(tǒng)的學(xué)習(xí)過,最近把Html5系統(tǒng)的擼了一遍,把一些學(xué)習(xí)到的知識(shí)陸續(xù)更新...
- 引用文檔: HTML5本地存儲(chǔ)——IndexedDB(一:基本使用)[http://www.cnblogs.com...
- 說到html的存儲(chǔ)大家第一時(shí)間會(huì)想到cookie。 網(wǎng)站應(yīng)用如果想在瀏覽器端存儲(chǔ)用戶的部分信息,那么只能借助于...