欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HTML5 Web存储方式的localStorage和sessionStorage进行数据本地存储案例应用

程序员文章站 2023-11-27 10:45:58
localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用,sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时... 12-12-09...

使用html5 web存储的localstorage和sessionstorage方式进行web页面数据本地存储。

页面参考如下图,能将页面上的数据进行本地存储。并能读取存储的数据显示在页面上。

localstorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用。

sessionstorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同意可以使用,关闭浏览器之后数据就会消失。

某个博主的测试localstorage兼容情况,如下
chrome4+ 开始支持localstorage

firefox3.5+开始支持localstorage
firefox1.5+支持globalstorage

ie8+支持localstorage
ie7兼容模式支持localstorage
ie5.5+支持userdata

safari 4+ 支持localstorage
opera10.5+支持localstorage

HTML5 Web存储方式的localStorage和sessionStorage进行数据本地存储案例应用 

复制代码
代码如下:

<!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>
<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>