【js】【筆記】普通js常用

1、普通js常用

var util = {
 
    _sampleTypeListSessionKey: "_sampleTypeList20210930",//樣本類型列表本地存儲鍵值

    //返回上一個頁面
    breakpage: function (p) {
        if (p == undefined) {
            p = -1;
        }
        window.location.href = "javascript:history.go(" + p + ")";
    },

    convertTime: function (val) {
        var date;
        if (typeof val == 'string' && val.indexOf("Date") <= -1) {
            date = new Date(val);
        } else {
            var re = /-?\d+/;
            var m = re.exec(val);
            date = new Date(parseInt(m[0]));
        }
        return date;
    },

    formatTime: function (val, format) {
        if (val == '' || val == null) {
            return '';
        }
        var date;
        if (typeof val == 'string' && val.indexOf("Date") <= -1) {
            date = new Date(val);
        } else if (typeof val == 'object'){
            //時間格式
            date = val;
        }
        else {
            var re = /-?\d+/;
            var m = re.exec(val);
            date = new Date(parseInt(m[0]));
        }
        if (format || format != null) {
            var o = {
                "M+": date.getMonth() + 1, //month
                "d+": date.getDate(), //day
                "H+": date.getHours(), //hour
                "m+": date.getMinutes(), //minute
                "s+": date.getSeconds(), //second
                "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
                "S": date.getMilliseconds() //millisecond
            }
            if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
                (date.getFullYear() + "").substr(4 - RegExp.$1.length));

            for (var k in o)
                if (new RegExp("(" + k + ")").test(format))
                    format = format.replace(RegExp.$1,
                        RegExp.$1.length == 1 ? o[k] :
                            ("00" + o[k]).substr(("" + o[k]).length));
        }
        return format;
    },

    //表單提交重定向
    formSubmitRedrict: function (url, datas, method, target, bc) {
        var tempFormName = 'tempForm';
        for (var i = 0; document.getElementById(tempFormName) != undefined; i++) {
            tempFormName = 'tempForm' + i;
        }
        var tempForm = document.createElement("form");
        tempForm.id = tempFormName;
        tempForm.method = method == undefined ? "post" : method;
        tempForm.action = url;
        tempForm.target = target == undefined ? "" : target;
        for (var i = 0; i < datas.length; i++) {
            var hideInput = document.createElement("input");
            hideInput.type = "hidden";
            hideInput.name = datas[i].name;
            hideInput.value = datas[i].value;
            tempForm.appendChild(hideInput);
        }

        if (bc == undefined) {
            bc = function () { }
        }
        if (document.all) {
            tempForm.attachEvent("onsubmit", bc);        //IE
        } else {
            tempForm.addEventListener("submit", bc);    //firefox
        }
        document.body.appendChild(tempForm);
        if (document.all) {
            tempForm.fireEvent("onsubmit");
        } else {
            tempForm.dispatchEvent(new Event("submit"));
        }
        tempForm.submit();
        document.body.removeChild(tempForm);
    },

    //copy到剪切板
    copyText: function (data, cb) {
        //data==={text: OrderNo,target:$event.target }
        //創建隱藏的標籤
        var textArea;
        try {
            textArea = document.createElement("<textarea></textarea>");
        } catch (e) {
            textArea = document.createElement("textarea");
        }
        textArea.textContent = data.text;

        //不顯示出來
        textArea.style.width = '1px';
        textArea.style.height = '1px';
        textArea.style.color = textArea.style.backgroundColor;

        //定位,不滾動
        if (!data.target) {
            data.target = document.body;
        }

        data.target.appendChild(textArea);

        textArea.select(); // 選擇對象

        var res = false;
        try {
            res = document.execCommand("copy");//複製
        } catch (err) {
            data.target.removeChild(textArea);
            console.log('該瀏覽器不支持點擊複製到剪貼板');
        }

        if (cb && typeof cb == "function") {
            cb(res)
        }
        data.target.removeChild(textArea);
    },

    //獲取樣本類型列表,保存在 瀏覽器session
    getSampleTypeList: function () {
        if (this._sampleTypeList.length <= 0) {
            var list = sessionStorage.getItem(this._sampleTypeListSessionKey);
            if (list) {
                this._sampleTypeList = JSON.parse(list);
                return this._sampleTypeList;
            }
            var _sampleTypeListSessionKey = this._sampleTypeListSessionKey;
            ajaxProtogenesis.ajax("post", "/Product/ProductInfo/GetProductExtractionSampleDTO", true, {}, function (res) {

                this._sampleTypeList = res.RetData;
                sessionStorage.setItem(_sampleTypeListSessionKey, JSON.stringify(this._sampleTypeList));
            })
        }
        return this._sampleTypeList;
    },
    _sampleTypeList: [],

    //獲取名稱公共方法,用於過濾器調用
    getSampleTypeName: function (id) {
        var list = this.getSampleTypeList();
        for (var i = 0; i < list.length; i++) {
            if (list[i].ExtractionSampleTypeId == id) {
                return list[i].SampleTypeName;
            }
        }
        return "";
    },
};

 

2、//微信小程序轉ng電腦端,複製代碼做部分中間處理

var wx = {
    //本地緩存
    setStorageSync: function (key, value) {
        if (typeof value == "object") {
            value = JSON.stringify(value);
        }
        localStorage.setItem(key, value);
    },
    getStorageSync: function (key) {
        var value = localStorage.getItem(key);
        try {
            return JSON.parse(value);
        } catch (e) {
        }
        return value;
    },
    removeStorageSync: function (key) {
        localStorage.removeItem(key);
    },

    //跳轉
    navigateTo: function (data) {
        document.location.href = data.url;
    },
    navigateBack: function (data) {
        if (!data) {
            util.breakpage(-1);
            return;
        }
        util.breakpage((0 - data.delta));
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章