zoukankan      html  css  js  c++  java
  • 临时存存储页面上的数据---Web存储

    HTML5 Web存储的两种方法使用

    localStorage和sessionStorage

    参考:

    http://www.cnblogs.com/taoweiji/archive/2012/12/08/2808997.html

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <style type="text/css">
            textarea {
                 300px;
                height: 300px;
            }
    
            .button {
                 100px;
            }
        </style>
    </head>
    <body onload="init()">
        <script type="text/javascript">
            //使用HTML5 Web存储的localStorage和sessionStorage方式进行Web页面数据本地存储。
            //页面参考如下图,能将页面上的数据进行本地存储。并能读取存储的数据显示在页面上。
            var t1;
            var t2;
            var oStorage;
            var oStorage;
            function init() {//初始化t1,t2
                t1 = document.getElementById("t1");
                t2 = document.getElementById("t2");
                sStorage = window.sessionStorage;
                lStorage = window.localStorage
            }
            function saveSession() {
                sStorage.mydata = t2.value;
                t1.value += "sessionStorage保存mydata:" + t2.value + "
    ";
            }
            function readSession() {
                t1.value += "sessionStorage读取mydata:" + sStorage.mydata + "
    ";
            }
            function cleanSession() {
                t1.value += "sessionStorage清除mydata:" + sStorage.mydata + "
    ";
                sStorage.removeItem("mydata");
            }
            function saveStorage() {
                lStorage.mydata = t2.value;
                t1.value += "localStorage保存mydata:" + t2.value + "
    ";
            }
            function readStorage() {
                t1.value += "localStorage读取mydata:" + lStorage.mydata + "
    ";
            }
            function cleanStorage() {
                t1.value += "localStorage清除mydata:" + lStorage.mydata + "
    ";
                lStorage.removeItem("mydata");
            }
        </script>
        <div>
            <textarea id="t1"></textarea><br />
            <label>需要保存的数据: </label>
            <input type="text" id="t2" /><br />
            <input type="button" class="button" onclick="saveSession()" name="b1" value="session保存" />
            <input type="button" class="button" onclick="readSession()" value="session读取" />
            <input type="button" class="button" onclick="cleanSession()" value="session清除" /><br />
            <input type="button" class="button" onclick="saveStorage()" value="local保存" />
            <input type="button" class="button" onclick="readStorage()" value="local读取" />
            <input type="button" class="button" onclick="cleanStorage()" value="local清除" />
        </div>
    </body>
    </html>
  • 相关阅读:
    cookie封装
    禁止网站某页面被频繁刷新(验证)
    $(...) is null
    svn: E155004: Working copy '/data/www' locked.
    svn checkout不带根文件夹方法
    IIS LUA推荐
    给Elasticsearch 5.2.2 设置用户权限 how to setting security for elasticsearch on windows
    怎么部署 .NET Core Web项目 到linux
    怎么删除Elasticsearch里的index内容
    Windows 计划任务 Task Schedule 怎么 运行 .bat文件
  • 原文地址:https://www.cnblogs.com/hjt-7/p/5795711.html
Copyright © 2011-2022 走看看