• Js 的常用方法:页面跳转,Session,类继承


    MyApp.Base = function () {
    }
    var basePrototype = MyApp.Base["prototype"];
    //对象克隆方法
    basePrototype["Clone"] = function (myObj) {
        if (typeof (myObj) != 'object') return myObj;
        if (myObj == null) return myObj;
    
        var myNewObj = new Object();
    
        for (var i in myObj)
            myNewObj[i] = this.Clone(myObj[i]);
    
        return myNewObj;
    }
    //页面导航方法
    basePrototype["Navigate"]=function(url)
    {
        window.location.href = url;
    }
    //页面导航方法
    basePrototype["NavigateTag"] = function (tag) {
        switch (tag) {
            case "myHome":
                this.Navigate(bootPATH + "app/html/myhome.html");
                break;
            case "zone":
                this.Navigate(bootPATH + "app/html/header_sample.html");
                break;
        }
    }
    
    basePrototype["SetSession"] = function (keyname, keyvalue) {
        var Days = 30;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = keyname + "=" + escape(keyvalue) + ";expires=" + exp.toGMTString();
    }
    basePrototype["GetSession"] = function (keyname) {
        var aCookie = document.cookie.split("; ");
        var lastMatch = null;
        for (var i = 0; i < aCookie.length; i++) {
            var aCrumb = aCookie[i].split("=");
            if (keyname == aCrumb[0]) {
                lastMatch = aCrumb;
            }
        }
        if (lastMatch) {
            var v = lastMatch[1];
            if (v === undefined) return v;
            return unescape(v);
        }
        return null;
    }
    basePrototype["ClearSession"] = function (keyname) {
        if (keyname == "") {
            window.localStorage.clear();
        }
        else {
            document.cookie = null;
        }
    }
    basePrototype["Extend"] = function (subClass, superClass) {
        var F = function () { };
    
        F.prototype = superClass.prototype;
    
        subClass.prototype = new F();
    
        subClass.prototype.constructor = subClass;
    
        subClass.superclass = superClass.prototype; //加多了个属性指向父类本身以便调用父类函数
    
        if (superClass.prototype.constructor == Object.prototype.constructor) {
    
            superClass.prototype.constructor = superClass;
    
        }
    };
    //定义全局的Base类对象
    var MyAppBase = new MyApp.Base();


    用法如下:

    //设置Session
    MyAppBase.SetSession("Username", "wahaha");
    //如果设置的是一个实体类,则需要用JSON.stringify把实体转化为Json字符串保存
    MyAppBase.SetSession("User", JSON.stringify(UserItem));
    
    //获取Session
    MyAppBase.GetSession("Username");
    //用JSON.parse把Json字符串转化为实体
    JSON.parse(MyAppBase.GetSession("User"));
    
    //跳转页面
    MyAppBase.Navigate(bootPATH + "app/html/cctvlist.html");
    
    //继承类
    MyAppBase.Extend(ChildClass , ParentClass);
  • 相关阅读:
    js实现element中可清空的输入框(1)
    vue中v-model的学习
    Oracle触发器和MySQL触发器的简单应用
    Java中的多线程你只要看这一篇就够了()
    HTML 随笔记之 刷新页面
    HTML CSS 随笔记之 cursor
    float到底是干什么的?
    Spring Boot 事物的快速应用
    java web 中文乱码
    LVM 创建实例
  • 原文地址:https://www.cnblogs.com/lizeyan/p/3512256.html
Copyright © 2020-2023  润新知