html5的本地存储,html5本地存储 local storage

The localStorage Object

The localStorage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.

localStorage 方法存储的数据没有时间限制。第二天、第二周或下一年之后,数据依然可用。

如何创建和访问 localStorage:

localStorage.lastname="Smith";

document.getElementById("result").innerHTML="Last name: "

+ localStorage.lastname;

Example explained:

Create a localStorage key/value pair with key="lastname" and value="Smith"

Retrieve the value of the "lastname" key and insert it into the element with id="result"

Tip: Key/value pairs are always stored as strings. Remember to convert them to another format when needed.

key/value总是作为字符串存储,记住需要时转换他们。

The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number to be able to increase the counter:

function clickCounter()

{if(typeof(Storage)!=="undefined")

{if(localStorage.clickcount)

{

localStorage.clickcount=Number(localStorage.clickcount)+1;

}else{

localStorage.clickcount=1;

}

document.getElementById("result").innerHTML="You have clicked the button" + localStorage.clickcount + "time(s).";

}else{

document.getElementById("result").innerHTML="Sorry, your browser does not support web storage...";

}

}

Click me!

The sessionStorage Object

The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the browser window.

sessionStorage当用户关闭浏览器后,删除。

The following example counts the number of times a user has clicked a button, in the current session:

if(sessionStorage.pagecount)

{

sessionStorage.pagecount=Number(sessionStorage.pagecount)+1;

}else{

sessionStorage.pagecount=1;

}

document.write("Visits"+sessionStorage.pagecount+"time(s) this session.");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值