HTML5 Web储存方法的localStorage和sessionStorage开展数据

日期:2021-02-26 类型:科技新闻 

关键词:秀米制作h5,h5如何制作,h5小游戏制作,h5游戏开发,免费的h5制作网站

应用HTML5 Web储存的localStorage和sessionStorage方法开展Web网页页面数据信息当地储存。

网页页面参照以下图,能将网页页面上的数据信息开展当地储存。并能载入储存的数据信息显示信息在网页页面上。

localStorage(当地储存),能够长期性储存数据信息,沒有時间限定,1天,1年,两年乃至更长,数据信息都可以以应用。

sessionStorage(对话储存),仅有在访问器被关掉以前应用,建立另外一个网页页面时愿意可使用,关掉访问器以后数据信息就会消退。

某个博主的检测localStorage适配状况,以下
Chrome4+ 刚开始适用localStorage

Firefox3.5+刚开始适用localStorage
Firefox1.5+适用globalStorage

IE8+适用localStorage
IE7适配方式适用localStorage
IE5.5+适用userdata

Safari 4+ 适用localStorage
Opera10.5+适用localStorage

 

拷贝编码
编码以下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf⑻" />
<title></title>
<style type="text/css">
textarea {
width: 300px;
height: 300px;
}
.button {
width: 100px;
}
</style>
</head>
<body>
<script type="text/javascript">
//应用HTML5 Web储存的localStorage和sessionStorage方法开展Web网页页面数据信息当地储存。
//网页页面参照以下图,能将网页页面上的数据信息开展当地储存。并能载入储存的数据信息显示信息在网页页面上。
function saveSession() {
var t1 = document.getElementById("t1");
var t2 = document.getElementById("t2");
var mydata = t2.value;
var oStorage = window.sessionStorage;
oStorage.mydata = mydata;
t1.value += "sessionStorage储存mydata:" + mydata + "\n";
}
function readSession() {
var t1 = document.getElementById("t1");
var oStorage = window.sessionStorage;
var mydata = "不存在";
if (oStorage.mydata) {
mydata = oStorage.mydata;
}
t1.value += "sessionStorage载入mydata:" + mydata + "\n";
}
function cleanSession() {
var t1 = document.getElementById("t1");
var oStorage = window.sessionStorage;
var mydata = "不存在";
if (oStorage.mydata) {
mydata = oStorage.mydata;
}
oStorage.removeItem("mydata");
t1.value += "sessionStorage消除mydata:" + mydata + "\n";
}
function saveStorage() {
var t1 = document.getElementById("t1");
var t2 = document.getElementById("t2");
var mydata = t2.value;
var oStorage = window.localStorage;
oStorage.mydata = mydata;
t1.value += "localStorage储存mydata:" + mydata + "\n";
}
function readStorage() {
var t1 = document.getElementById("t1");
var oStorage = window.localStorage;
var mydata = "不存在";
if (oStorage.mydata) {
mydata = oStorage.mydata;
}
t1.value += "localStorage载入mydata:" + mydata + "\n";
}
function cleanStorage() {
var t1 = document.getElementById("t1");
var oStorage = window.localStorage;
var mydata = "不存在";
if (oStorage.mydata) {
mydata = oStorage.mydata;
}
oStorage.removeItem("mydata");
t1.value += "localStorage消除mydata:" + mydata + "\n";
}
</script>
<div>
<textarea id="t1"></textarea>
<label>必须储存的数据信息: </label>
<input type="text" id="t2" />
<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消除" />
<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>