日期:2014-05-17  浏览次数:20644 次

用HTML5的本地存储功能

下面的例子展示了在HTML5应用程序中你如何保存数据在客户端本地,使用localStorage属性。

?

<!DOCTYPE HTML>
<html>
<head>
    <title>Using local storage in HTML5</title>
</head>
<body onload="incrementCounter();">
 
    <div id="message"></div>
    <button onclick="clearCounter();">Clear localStorage <em>and reload page</em></button>
    <button onclick="window.location.reload();">Reload page</button>
    <p id="timestamp"></p>
 
    <script type="text/javascript">
        function incrementCounter() {
            var message;
            if (('localStorage' in window) && (window.localStorage !== null)) {
                if (!localStorage.counter) {
                    localStorage.counter = 0;
                }
                localStorage.counter++;
                var suffix = (localStorage.counter == 1) ? "time" : "times";
                message = "<h1>You've accessed this page <em>" + localStorage.counter + "</em> " + suffix + "</h1>";
            } else {
                message = "<p>Your browser does not seem to support <code>localStorage</code>, try using <a href=\"http://www.google.com/chrome\">Google Chrome</a>.</p>";
            }
            document.getElementById("message").innerHTML = message;
            document.getElementById("timestamp").innerHTML = "Last modified: <strong>" + new Date().toLocaleTimeString() + "</strong>";
        }
        function clearCounter() {
            localStorage.clear();
            alert("Clearing the local storage");
            window.location.reload();
        }
    </script>
 
</body>
</html>

?源码下载

HTML5使用本地存储using-local-storage-in-html5.zip

?