日期:2014-05-17  浏览次数:20698 次

html5 localStorage的使用

?

参考文章:

http://www.iteye.com/topic/983810

?

<script type="text/javascript">

if (window.localStorage) {

//alert(window.localStorage);

//alert('internet exploer is support local storage!');

} else {

//alert('internet exploer is no support local storage!');

}

?

if (window.sessionStorage) {

//alert(window.sessionStorage);

//alert('internet exploer is support session storage!');

} else {

//alert('internet exploer is no support session storage!');

}

//html5本地存储

//设置变量

window.localStorage.a = 'liuhui';

//alert(window.localStorage.a);

localStorage.b = 'liuhuib';

//alert(localStorage.b);

localStorage.a = 'liuhuia';

//alert(localStorage.a);

localStorage.setItem('c', 'liuhuic');

//alert(localStorage.c);

//alert(localStorage.getItem('c'));

localStorage.removeItem('c');//c value is null

//alert(localStorage.getItem('c'));

//localStorage.clear();//

//alert(localStorage.b);//note property b so value undefined

/*

我测试了一下。localStorage.removeItem(property)

和localStorage.clear();//清空所有

有一点差别:

removeItem 对象localStorage来说属性仍然存在,只是值为null

而使用clear(),则没有该属性了,alert(localStorage.property) 输出undefined

或者说改属性赋值undefined

*/

for ( var j = 0; j < localStorage.length; j++) {

alert(localStorage.key(j) + ' --- '

+ localStorage.getItem(localStorage.key(j)));

?

}

?

?

?

//localStorage example:

/*

* 需要注意的是,HTML5本地存储只能存字符串,任何格式存储的时候都会被自动转为字符串,

所以读取的时候,需要自己进行类型的转换。

这也就是上一段代码中parseInt必须要使用的原因。

*/

?

//var myDiv = document.createElement('DIV');

var calculateNumberStr = localStorage.getItem('count');

if (calculateNumberStr) {//'0' experssion is true that used boolean runtime

localStorage.setItem('count', 0);