<script type="text/javascript"> // 验证当前的浏览器是否支持localStorage和sessionStorage window.onload = function(){ if (window.localStorage && window.sessionStorage) { alert("你的浏览器支持localStorage和sessionStorage"); } } </script>
<script type="text/javascript"> // 创建均使用localStorage做示例,sessionStorage语法方式和localStorage是一样的,localStorage(key, value);或者localStorage.key = "value"; window.onload = function(){ if (window.localStorage) { localStorage.setItem("userName", "张三"); } } </script>
<script type="text/javascript">
// 可以直接使用localStorage.getItem(key);也可以使用localStorage.key;
window.onload = function(){
if (window.localStorage && window.sessionStorage) {
localStorage.setItem("userName", "张三");
alert(localStorage.getItem("userName"));
}
}
</script>
<script type="text/javascript">
// localStorage.removeItem(key):根据键名删除指定存储,localStorage.clear():删除指定所有存储,当Storage对象为空时,也是可执行的。
window.onload = function(){
if (window.localStorage && window.sessionStorage) {
localStorage.setItem("userName", "hello,world!");
localStorage.removeItem("userName");
localStorage.clear();
}
}
</script>