HTML5 localStorage

Html5新出的Storage对象,分成2种:sessionStorage和localStorage。注意:首字母都是小写--->window.sessionStorage和window.localStorage

API一致:getItem, setItem, clear, length, removeItem, key

sessionStorage.key(1)--->返回第2个item的key

 

试验了localStorage,发现大多数浏览器都能用这个对象了

无论是刷新页面也好,关闭浏览器也好,都会remember住localStorage的values

代码如下:

<body>
    <label id="lbl">-</label>
    <input type="button" onclick="javascript:AddOne();RefreshCount();" value="Add" />
    <script language="javascript" type="text/javascript">
    <!--
        function AddOne() {
            if (window.localStorage.Count == null || window.localStorage.Count == undefined)
                window.localStorage.Count = 0;
            window.localStorage.Count = parseInt(window.localStorage.Count) + 1;
        }

        function RefreshCount() {
            document.getElementById("lbl").innerHTML = window.localStorage.Count;
        }

        if (window.localStorage == null || window.localStorage == undefined)
            alert("window.localStorage not support.");
        else
            RefreshCount();
    //-->
    </script>
</body>

 

posted @ 2013-03-22 13:53  McKay  阅读(1157)  评论(0编辑  收藏  举报