天天看點

h5 緩存

h5 緩存工具類

// 應用全局對象

var MyApp = new CMyApp();

function CMyApp(){   

    this.Storage = new CAppStorage();

}

// 本地存儲

function CAppStorage() {

    //儲存對象到本地存儲

    this.setObject = function(key, obj) {

            var str = null;

            if (obj != null && obj != undefined) {

                str = JSON.stringify(obj);

            }

            localStorage.setItem(key, str);

        }

    //儲存對象到session

    this.setObjectSession = function(key, obj) {

        var str = null;

        if (obj != null && obj != undefined) {

            str = JSON.stringify(obj);

        }

        sessionStorage.setItem(key, str);

    }

    //清空session

    this.getObjectSessionclear = function() {

            sessionStorage.clear();

        }

        //儲存字元串到本地存儲

    this.setValuelocal = function(key, str) {

        localStorage.setItem(key, str);

    };

    //擷取本地存儲的字元串,如果找不到傳回''

    this.getValuelocal = function(key) {

        var str = localStorage.getItem(key);

        if (str == undefined || str == null) str = '';

        return str;

    };

    //儲存字元串到session

    this.setValueSession = function(key, str) {

        sessionStorage.setItem(key, str);

    };

    //擷取session的字元串,如果找不到傳回

    this.getValueSession = function(key) {

        var str = sessionStorage.getItem(key);

        if (str == undefined || str == null) str = '';

        return str;

    };

    //bool工具

    this.getBool = function(key) {

        var str = this.getValue(key).toLowerCase();

        if (str == '1' || str == 'true' || str == 't' || str == 'yes') return true;

        return false;

    }

    //擷取本地存儲的對象

    this.getObject = function(key) {

        var str = localStorage.getItem(key);

        if (str == undefined || str == null || str.length == 0) return null;

        try {

            var obj = eval('(' + str + ')');

            return obj;

        } catch (e) {

            return null;

        }

    };

    //擷取session存儲的對象

    this.getObjectSession = function(key) {

        var str = sessionStorage.getItem(key);

        if (str == undefined || str == null || str.length == 0) return null;

        try {

            var obj = eval('(' + str + ')');

            return obj;

        } catch (e) {

            return null;

        }

    };

    //移除指定的本地存儲

    this.remove = function(key) {

        localStorage.removeItem(key);

    };

    //清空本地存儲

    this.clear = function() {

        localStorage.clear();

    };

    this.getDefaultLoginUsr = function() {

        return this.getValue('DefaultUsrCode');

    };

    this.getDefaultLoginPassword = function() {

        return this.getValue('DefaultPassword');

    };

};

1。html登入列子

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <title></title>

        <script src="jquery.min.js"></script>

        <script src="storage.com.js"></script>

        <script src="login.js"></script>

    </head>

    <body>

        <input type="text" id="name"/>

        <input type="text" id="pass"/>

        <button οnclick="login()">登入</button>

    </body>

</html>

2 。js 如何使用

function login(){

    var name = $("#name").val();

    var pass = $("#pass").val();

    MyApp.Storage.setValuelocal("name",name);

    MyApp.Storage.setValuelocal("pass",pass);

    MyApp.Storage.setValueSession("names",name);

    MyApp.Storage.setValueSession("passs",pass);

    window.location.href="ok.html" target="_blank" rel="external nofollow"

}

3.html 成功界面

<html>

    <head>

        <meta charset="UTF-8">

        <title></title>

        <script src="jquery.min.js"></script>

        <script src="storage.com.js"></script>

        <script src="ok.js"></script>

    </head>

    <body>

        localStorage 本地緩存

        <input type="text" id="name" />

        <input type="text" id="pass" />

        sessionStorage sessionh緩存

        <input type="text" id="names" />

        <input type="text" id="passs" />

    </body>

</html>

4.js 取值

$(function(){

        var name = MyApp.Storage.getValuelocal('name');

        var pass =MyApp.Storage.getValuelocal('pass');

        var names = MyApp.Storage.getValueSession('names');

        var passs =MyApp.Storage.getValueSession('passs');

        $("#name").attr("value",name);

        $("#names").attr("value",names);

        $("#pass").attr("value",pass);

        $("#passs").attr("value",passs);

});

列子下載下傳位址http://download.csdn.net/detail/qq_21170031/9542395

繼續閱讀