您的位置:首页 > Web前端 > HTML5

HTML5的新存储方式

2012-05-25 10:23 330 查看
1.sessionStorage

两个页面a.html,b.html

## a.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
sessionStorage.setItem('msg','hello world');
console.log(sessionStorage.getItem('msg'));
</script>
</body>
</html>
打印结果 是 : hello world


## b.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
console.log(sessionStorage.getItem('msg'));
</script>
</body>
</html>

打印结果 是 : null


个人感觉sessionStroage和一个js的变量差不多,作用域是整个页面

2. localStroage

#a.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
localStorage.setItem('msg','hello world');
console.log(localStorage.getItem('msg'));
</script>
</body>
</html>

#结果hello world


# b.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
console.log(localStorage.getItem('msg'));
</script>
</body>
</html>
#结果输出 hello world

#当b.html的不在同一人域名下时输出为null


结论: localStroage和cookie的作用域一样
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: