1.Web Storage分为两种:
(1)Local Storage:保存再用户磁盘是Web Storage.
(2)Local Storage保存的生存期很长,除非用户或程序显示地清除这些数据.
(3)Storage提供了以下属性和方法:
1)length:返回Storage里保存了多少Key---value对
2)Key(index):返回Storage中第index个Key.
3)getItem(Key):返回Storage中指定Key对应地value值.
4)setItem(Key,value):向Storage中存入指定的Key-value的值.
5)removeItem(Key):删除Key对应的Key-value的值.
6)clear():删除Storage中所有Key-value值对.
2.本地存储的功能和作用:
(1)传统浏览器作用:
1)传统浏览器只是“界面呈现工具”,html向服务器发送请求,并读取服务器响应的html文档,负责呈现html文档.
2)如果浏览器处于离线状态,浏览器无法向服务器发送请求.
(2)现在浏览器(h5):
1)离线应用:控制浏览器缓存哪些页面,离线状态也可以看到页面.
2)Web Storage:在离线状态下,可以存储用户向服务器提交信息,当再一次连接服务器时自动提交.
//先设置在进行提取
$(function() {
$.ajax({
url: 'http://www.qhdlink-student.top/student/coacha.php',
type: 'post',
dataType: 'json',
data: { username: 'cty', userpwd: 12345678, userclass: 67, type: 2 },
success: function(data) {
if (data == "ok") {
localStorage.setItem("tokenc", "ok");
localStorage.href = "../6.21.html"; //跳转到主页面
} else {
}
}
})
})
if (localStorage.getItem("tokenc") != "ok") {
location.href = "./6.21.html";
alert("请登录后再进入系统!")
}
(3)安全退出:
HTML:
<input type="button" value="安全退出" name="out" onclick="outmain()">
JS:
function outmain() {
localStorage.clear();
location.href = "./6.21.html";
}