HTML5本地存储应用sessionStorage和localStorage


本文摘自PHP中文网,作者大家讲道理,侵删。

在html5之前,浏览器要实现数据的存储,一般都是用cookie,但是cookie有域名和大小限定.

html5流行之后,可以通过localStorage和sessionStorage实现浏览器端的数据存储,这两者有什么特点呢?

sessionStorage
  sessionStorage属于临时会话,数据存储的有效期为:从页面打开到页面关闭的时间段,属于窗口的临时存储,页面关闭,本地存储消失

localStorage

  • 永久存储(可以手动删除数据)

  • 存储量限制 ( 5M )

  • 客户端完成,不会请求服务器处理

  • sessionStorage数据在页面之间不能共享、 而localStorage可以实现页面之间共享

sessionStorage的应用:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title></title>

    <script>

        window.onload = function(){

            var aInput = document.getElementsByTagName('input');

            aInput[0].onclick = function(){

                //sessionStorage: 临时存储, 只在当前页面有效,不能传递到其他页面,页面关闭之后消失

                window.sessionStorage.setItem("name", aInput[3].value );

            };

            aInput[1].onclick = function(){

                alert(window.sessionStorage.getItem("name" ));

            };

            aInput[2].onclick = function(){

                window.sessionStorage.removeItem("name" );

            };

        }

    </script>

</head>

<body>

<input type="button" value="设置" />

<input type="button" value="获取" />

<input type="button" value="删除" />

<br/>

<input type="text" />

</body>

</html>


localStorage的应用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title></title>

    <script>

        window.onload = function(){

            var aInput = document.getElementsByTagName('input');

            aInput[0].onclick = function(){

                //localStorage : 永久性存储

                window.localStorage.setItem("name", aInput[3].value);

                window.localStorage.setItem("name2", 'aaaaa');

            };

            aInput[1].onclick = function(){

                alert( window.localStorage.getItem( "name" ) );

                alert( window.localStorage.getItem( "name2" ) );

            };

            aInput[2].onclick = function(){

                window.localStorage.removeItem("name");

//                window.localStorage.clear();

            };

        }

    </script>

</head>

<body>

<input type="button" value="设置" />

<input type="button" value="获取" />

<input type="button" value="删除" />

<br/>

<input type="text" />

</body>

</html>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title></title>

    <script>

        window.onload = function () {

            var aInput = document.getElementsByTagName("input");

            var oT = document.querySelector("textarea");

 

            if (window.localStorage.getItem("userName")) {

                aInput[0].value = window.localStorage.getItem("userName");

            }

 

            for (var i = 0; i < aInput.length; i++) {

                if (window.localStorage.getItem('sex') == aInput[i].value) {

                    aInput[i].checked = true;

                }

            }

 

            if (window.localStorage.getItem("note")) {

                oT.value = window.localStorage.getItem("note");

            }

 

            window.onunload = function () {

                if (aInput[0].value) {

                    window.localStorage.setItem("userName", aInput[0].value);

                }

 

                for (var i = 0; i < aInput.length; i++) {

                    if (aInput[i].checked == true) {

                        window.localStorage.setItem('sex', aInput[i].value);

                    }

                }

 

                if (oT.value) {

                    window.localStorage.setItem('note', oT.value);

                }

            }

        }

    </script>

</head>

<body>

<p>

    用户名: <input type="text"/>

</p>

 

<p>

    性别: <br/>

    <input type="radio" name="sex" value="男"/>男

    <input type="radio" name="sex" value="女"/>女

</p>

 

<p>

    备注:

    <textarea cols="30" rows="10"></textarea>

</p>

 

</body>

</html>

以上就是HTML5本地存储应用sessionStorage和localStorage的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

html5 canvas的绘制文本自动换行的示例代码

什么是h5页面

echarts-基于html5 canvas的javascript图表库图文详解

如何使用html5 canvas实现图像的马赛克

详细介绍7款炫酷的html5 canvas动画特效

分享h5调用摄像头实现拍照的实例教程

html5跨域信息交互技术之postmessage代码实例详解

html5初窥2之新元素

css中display: inline-block的用法解析

html5在工作中主要干什么

更多相关阅读请进入《sessionStorage》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...