localstorage2

将文本框的内容在关闭刷新页面前保存在本地,重新打开时回复保存的内容。可以用 sessionStorage代替localStorage与session用户名绑定。

        var Storage = (function () {
            var $this

            function storage() {
                this.ini.apply(this, arguments)
            }

            storage.prototype = {
                ini:function () {
                    $this = this
                },
                clear:function () {
                    window.localStorage.clear()
                },
                pri:function () {
                    for (var i = 0; i < localStorage.length; i++) {
                        document.write(localStorage.key(i) + " : " + localStorage.getItem(localStorage.key(i)) + "<br>");
                    }
                }
            }

            var k = new storage()
            k.pri()

        }())

        $(".ttd").click(function () {
            localStorage.clear()
        })

        window.onbeforeunload = function () {
            return false;
        }
        window.onunload = function () {
            var k = $("#text").val()
            localStorage.setItem("text", k)
        }
        $(document).ready(function () {
            if (localStorage.text) {
                $("#text").val(localStorage.text)
            }
        })

 

demo:http://fiddle.jshell.net/break_happy/ALzrB/4/show/light/

posted @ 2012-06-13 11:42  break_happy  Views(291)  Comments(0)    收藏  举报