Web storage存储基础知识
Web storage,
Localstorage
Sessionstorage:
1 检测浏览器是否支持数据存储
注意:在html5中 使用javascript 来存储和访问数据的。
2. Localstorage 设置 /获取数据
存储在客户端的(移动端 手机 平板…) 没有时间限制的
常用方法:
赋值:
window.localStorage .setItem(“变量名称”,”val”) ---》localStorage.变量名称 = 值
取值:
window.localStorage.getItem(“变量名称”)-à localStorage.变量名称
注意:测试localStorage 需要到 php环境下测试
3.sessionstorage 设置/获取数据
存储在服务器端(sohu 美食天下)
赋值:
window. sessionStorage .setItem(“变量名称”,”val”) ---》sessionStorage .变量名称 = 值
取值:
window. sessionStorage .getItem(“变量名称”)-à sessionStorage .变量名称
sessionStorage:
关闭浏览器
调用:sessionStorage.removeItem(“bName”); 清空数据
案列代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<link rel="stylesheet" href="css/style.css">
</head>
<script type="text/javascript">
//window.localStorage /sessionStorage
//if(true) true 不空 0 0.0 “0” window.localStorage 有值
if(window.localStorage){
alert(111);
}
if(window.sessionStorage){
alert(222);
}
</script>
<body>
</body>
</html>