• localstorage使用


     
    (function () {
        window.Storage = (function () {
            //有关flash的
            var flash = {
                id: "Storage_Flash_",
                path: "/Resource/Js/storage-flash.swf",
                cache: function () { return document.getElementById(this.id) },
                insert: function () {
                    if (this.cache()) { return true; } //如果有了不用新建了
                    var html = [];
                    try {//解决遨游的一个bug
                        if (external.max_language_id != undefined) {
                            this.path += "?random=" + Math.random();
                        }
                    }
                    catch (ex) { }
                    if ("ActiveXObject" in window) {
                        html.push("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' " +
                        "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/" +
                        "swflash.cab#version=10,0,0,0' id='" + this.id + "' width='1' height='1'>");
                        html.push("<param name='allowScriptAccess' value='always' />");
                        html.push("<param name='movie' value='" + this.path + "' />");
                        html.push("</object>");
                    } else {
                        html.push("<embed id='" + this.id + "' name='" + this.id + "' src='" + this.path + "' " +
                        "width='1' height='1' align='middle' allowScriptAccess='always' " +
                        "type='application/x-shockwave-flash' " +
                        "pluginspage='http://www.adobe.com/go/getflashplayer_cn' />");
                    };
                    var container = document.createElement("div");
                    container.style.position = "absolute";
                    container.style.top = "-999px";
                    container.innerHTML = html.join("");
                    try {
                        //document.documentElement.appendChild(container);
                        document.documentElement.insertBefore(container, document.documentElement.firstChild);
                        return true;
                    }
                    catch (ex) { return false; }
                },
                version: function () {
                    var version = 0, n = navigator;
                    if (n.plugins && n.plugins.length) {
                        for (var i = 0; i < n.plugins.length; i++) {
                            if (n.plugins[i].name.indexOf("Shockwave Flash") != -1) {
    
                                version = n.plugins[i].description.split("Shockwave Flash")[1];
                                break;
                            }
                        }
                    } else if ("ActiveXObject" in window) {
                        for (var i = 15; i > 7; i--) {
                            try {
                                if (new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i)) {
                                    version = parseInt(i);
                                    break;
                                }
                            } catch (ex) { }
                        }
                    }
                    return parseInt(version);
                },
                check: function () {
                    var bool = false;
                    if (this.version() >= 10) {
                        bool = this.insert();
                    }
                    return bool;
                }
            };
            var o = {
                ls: (typeof window.localStorage == "object") ? true : false, //再支持localstorage
                t: (navigator.appVersion.toString().indexOf("MSIE") > 0
                    && navigator.appVersion.toString().indexOf("MSIE 9.0") < 0) ? true : false,
                //ie9以下版本,在本地无法保存,采用服务器保存的方案
                g: (window.google && google.gears && google.gears.factory.create) ? true : false, //有gears再支持gears
                gs: (typeof window.globalStorage == "object") ? true : false, //再支持globalStorage
                f: (1 == 2) ? true : false, //有flash支持flashStorage//flash.check()
                u: false  //ie9以下版本,由于不支持跨页,所以放弃
            };
            //alert(o.ls+","+o.t+","+o.g+","+o.gs);
            //gears方案
            var g = (function () {
                var db = (o.g) ? google.gears.factory.create("beta.database", "1.0") : null;
                return {
                    Set: function (name, key, val) {
                        var complete = false;
                        db.open("beta.database");
                        with (db) try {
                            execute("create table if not exists storage" + name + " (itemkey text unique not null PRIMARY key,itemval text not null)");
                            var rs = execute("select itemkey from storage" + name + " where itemkey=?", [key]);
                            if (rs.isValidRow()) { execute("update storage" + name + " set itemval=? where itemkey=?", [val, key]); } else { execute("insert into storage" + name + " values (?,?)", [key, val]); };
                            rs.close();
                            complete = true;
                        } catch (e) { }; //alert(e.message);
                        db.close();
                        return complete;
                    },
                    Get: function (name, key) {
                        var backval = "";
                        db.open("beta.database");
                        try {
                            var rs = db.execute("select itemkey,itemval from storage" + name + " where itemkey=?", [key]);
                            if (rs.isValidRow()) {
                                backval = rs.field(1);
                                backval = (backval == null || backval == undefined) ? "" : backval;
                            }
                            rs.close();
                        } catch (e) { };
                        db.close();
                        return backval;
                    },
                    Del: function (name, key) {
                        var complete = false;
                        db.open("beta.database");
                        try {
                            db.execute("delete from storage" + name + " where itemkey=?", [key]);
                            complete = true;
                        } catch (e) { };
                        db.close();
                        return complete;
                    },
                    Remove: function (name) {
                        var complete = false;
                        db.open("beta.database");
                        try {
                            db.execute("drop table storage" + name);
                            complete = true;
                        } catch (e) { };
                        close();
                        return complete;
                    }
                }
            })();
    
            //localStorage和globalStorage方案(二者类似firefox,chrome,opear,ie8/9支持localStorage)
            var gs, ls;
            gs = ls = (function () {
                var storage = (o.ls) ? window.localStorage : (o.gs) ? window.globalStorage[location.hostname] : null;
                return {
                    Set: function (name, key, val) {
                        var loadval = storage.getItem(name);
                        if (loadval == undefined || loadval == null) {
                            storage.setItem(name, key + "=" + val);
                        } else {
                            var array = loadval.split(";");
                            var len = array.length;
                            var addnew = true;
                            for (var i = 0; i < len; i++) {
                                if (array[i].indexOf(key + "=") == 0) {
                                    array[i] = key + "=" + val;
                                    addnew = false;
                                    break;
                                }
                            }
                            if (addnew) {
                                array.push(key + "=" + val);
                            }
                            storage.setItem(name, array.join(";"));
                        }
                        return true;
                    },
                    Get: function (name, key) {
                        var backval = "";
                        var loadval = storage.getItem(name);
                        if (loadval != undefined && loadval != null) {
                            var array = loadval.split(";");
                            var len = array.length;
                            for (var i = 0; i < len; i++) {
                                if (array[i].indexOf(key + "=") == 0) {
                                    backval = array[i].substr(array[i].indexOf("=") + 1);
                                    break;
                                }
                            }
                        }
                        return backval;
                    },
                    Del: function (name, key) {
                        return this.Set(name, key, "");
                    },
                    Remove: function (name) {
                        if (storage.getItem(name)) {
                            storage.setItem(name, "");
                        }
                        return true;
                    }
                }
            })();
    
            //flashShare方案,flash方案有一定的局限性,需要页面加载完成后才可以使用。
            var f = (function () {
                return {
                    //外部循环检查flash对象是否准备妥当
                    IsReady: function () {
                        return (flash.cache().set) ? true : false;
                    },
                    Set: function (name, key, val) {
                        flash.cache().set(name, key, val);
                    },
                    Get: function (name, key) {
                        return flash.cache().get(name, key);
                    },
                    Del: function (name, key) {
                        return flash.cache().del(name, key);
                    },
                    Remove: function (name) {
                        flash.cache().remove(name);
                    }
                }
            })();
    
            //userData方案ie6/7, 该方案不支持跨页。
            var u = (function () {
                if (o.u) document.documentElement.addBehavior("#default#userdata");
                return {
                    Set: function (name, key, val) {
                        with (document.documentElement) try {
                            load(name);
                            setAttribute(key, val);
                            save(name);
                            return true;
                        }
                        catch (e) {
                            return false;
                        }
                    },
                    Get: function (name, key) {
                        var backval = "";
                        with (document.documentElement) try {
                            load(name);
                            backval = getAttribute(key);
                            if (backval == undefined || backval == null) { backval = ""; }
                        } catch (e) { }
                        return backval;
                    },
                    Del: function (name, key) {
                        return this.Set(name, key, "");
                    },
                    Remove: function (name) {
                        with (document.documentElement) try {
                            load(name);
                            expires = new Date(315532799000).toUTCString();
                            save(name);
                            return true;
                        }
                        catch (e) { return false; }
                    }
                }
            })();
            
            //服务器端保存方案,在ie6.0下才是必须使用的方式
            var t=(function(){
                return{
                    Set:function(name,key,val){
                        var postdata = {"Do": "Set",name: name,Key:key,value: val};
                        var ret=false;
                        $.ajax({
                            url: "/Web/Handler1.ashx?action=SvrStorage",
                            type:"post",
                            data:postdata,
                            async: false,
                            success:function(){
                                ret=true;
                            },
                            error:function(){
                            }
                        });
                        return true;
                    },
                    Get:function(name,key){
                        var postdata = {"Do": "Get",name: name,Key:key};
                        var ret="";
                        $.ajax({
                            url: "/Web/Handler1.ashx?action=SvrStorage",
                            type:"post",
                            data:postdata,
                            async: false,
                            success:function(data){
                                ret=data;
                            },
                            error:function(){
                            }
                        });
                        return ret;
                    },
                    Del:function(name,key){
                        var postdata = {"Do": "Del",name: name,Key:key};
                        var ret=false;
                        $.ajax({
                            url: "/Web/Handler1.ashx?action=SvrStorage",
                            type:"post",
                            data:postdata,
                            async: false,
                            success:function(data){
                                ret=true;
                            },
                            error:function(){
                            }
                        });
                        return ret;
                    },
                    Remove:function(name){
                        var postdata = {"Do": "Remove",name: name};
                        var ret=false;
                        $.ajax({
                            url: "/Web/Handler1.ashx?action=SvrStorage",
                            type:"post",
                            data:postdata,
                            async: false,
                            success:function(data){
                                ret=true;
                            },
                            error:function(){
                            }
                        });
                        return ret;
                    }
                }
            })();
            //Public方法
            return {
                Set: function (name, key, val) {
                    for (var s in o) {
                        if (eval("o." + s)) {
                            return (eval(s + ".Set("" + name + "","" + key + "","" + encodeURIComponent(val) + "")"));
                        }
                    }
                },
                Get: function (name, key) {
                    for (var s in o) {
                        if (eval("o." + s)) {
                            return decodeURIComponent(eval(s + ".Get("" + name + "","" + key + "")"));
                        }
                    }
                },
                Del: function (name, key) {
                    for (var s in o) {
                        if (eval("o." + s)) {
                            return (eval(s + ".Del("" + name + "","" + key + "")"));
                        }
                    }
                },
                Remove: function (name) {
                    for (var s in o) {
                        if (eval("o." + s)) {
                            return (eval(s + ".Remove("" + name + "")"));
                        }
                    }
                },
                IsReady: function () {
                    for (var s in o) {
                        if (eval("o." + s)) {
                            if (s == "f") {//flash
                                return f.IsReady();
                            } else {
                                return true;
                            }
                            //return (eval(s + ".IsReady()"));
                        }
                    }
                }
            }
        })();
    })();

  • 相关阅读:
    转:浅谈Linux的内存管理机制
    (转)Redis
    检测socket链接是否断开
    linux C 中的volatile使用
    LINUX 下 ipv6 socket 编程
    linux signal 列表
    Linux下异常信号
    linux signal
    转: 关于Linux常用的二进制文件分析方法
    IOI2020 题解
  • 原文地址:https://www.cnblogs.com/dongqinglove/p/4612766.html
Copyright © 2020-2023  润新知