爱上歆随懿恫吧 关注:1贴子:55
  • 0回复贴,共1

sessionStorage和localStorage用法

只看楼主收藏回复

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
textarea {
width: 300px;
height: 300px;
}
.button {
width: 100px;
}
</style>
</head>
<body onload="session()">
<script type="text/javascript">
//使用HTML5 Web存储的localStorage和sessionStorage方式进行Web页面数据本地存储。
//页面参考如下图,能将页面上的数据进行本地存储。并能读取存储的数据显示在页面上。
var t1;
var t2;
function session() {
t1 = document.getElementById("t1");
t2 = document.getElementById("t2");
sStorage = window.sessionStorage;
lStorage = window.localStorage
}
function saveSession() {
sStorage.mydata = t2.value;
t1.value += "这是sessionStorage保存的值:" + t2.value + "\n";
}
function readSession() {
t1.value += "这是sessionStorage读取的值:" + sStorage.mydata + "\n";
}
function cleanSession() {
t1.value += "清除sessionStorage:" + sStorage.mydata + "\n";
sStorage.removeItem("mydata");
}
function saveStorage() {
lStorage.mydata = t2.value;
t1.value += "这是localStorage保存的值:" + t2.value + "\n";
}
function readStorage() {
t1.value += "这是localStorage读取的值:" + lStorage.mydata + "\n";
}
function cleanStorage() {
t1.value += "清除localStorage:" + lStorage.mydata + "\n";
lStorage.removeItem("mydata");
}
</script>
<div>
<textarea id="t1"></textarea><br />
<label>需要保存的数据: </label>
<input type="text" id="t2" /><br />
<input type="button" class="button" onclick="saveSession()" name="b1" value="session保存" />
<input type="button" class="button" onclick="readSession()" value="session读取" />
<input type="button" class="button" onclick="cleanSession()" value="session清除" /><br />
<input type="button" class="button" onclick="saveStorage()" value="local保存" />
<input type="button" class="button" onclick="readStorage()" value="local读取" />
<input type="button" class="button" onclick="cleanStorage()" value="local清除" />
</div>
</body>
</html>


IP属地:云南1楼2015-09-08 15:24回复