js一些常用方法

string 增加 IsNullorEmpty :

String.prototype.IsNullOrEmpty = function (r) {
    if (r === undefined || r === null || r === "") {
        return true;
    } else {
        return false;
    }
}

Json转String:

function JsonToString(o) {
    var arr = [];
    var fmt = function (s) {
        if (typeof s == 'object' && s != null) return JsonToStr(s);
        return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
    }
    for (var i in o) arr.push("'" + i + "':" + fmt(o[i]));
    return '{' + arr.join(',') + '}';
}

获取浏览器版本:

function GetExplorer() {
    var OsObject = "";
    if (navigator.userAgent.indexOf("MSIE") > 0) {
        return "IE";
    }
    if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
        return "Firefox";
    }
    if (isSafari = navigator.userAgent.indexOf("Safari") > 0) {
        return "Safari";
    }
    if (isCamino = navigator.userAgent.indexOf("Camino") > 0) {
        return "Camino";
    }
    if (isMozilla = navigator.userAgent.indexOf("Gecko/") > 0) {
        return "Gecko";
    }
}

操作日期的一些方法:

function FormatJsonTime(json) {
    if (json != null) {
        var date = ConvertJSONDateToJSDateObject(json);
        return GetTime(date);
    }
    else {
        return "";
    }
}
function FormatJsonDate(json) {
    if (json != null) {
        var date = ConvertJSONDateToJSDateObject(json);
        return GetDateTime(date);
    }
    else {
        return "";
    }
}

function FormatJsonDay(json) {
    if (json != null) {
        var date = ConvertJSONDateToJSDateObject(json);
        return GetDate(date);
    }
    else {
        return "";
    }
}

/* Json时间转换成JS时间对象 */
function ConvertJSONDateToJSDateObject(jsondate) {
    var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
    return date;
}

/* 获取日期 */
function GetDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return year + "-" + month + "-" + day;
}

/* 获取日期 */
function GetDate_(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return year + "/" + month + "/" + day;
}

/* 获取日期时间 */
function GetDateTime(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hh = date.getHours();
    var mm = date.getMinutes();
    var ss = date.getSeconds();
    return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
}
function GetTime(date) {
    var hh = date.getHours();
    var mm = date.getMinutes();
    var ss = date.getSeconds();
    return hh + ":" + mm + ":" + ss;
}

js产生UUID

function Str4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function UUID() {
    return Str4() + Str4() + "-" + Str4() + "-" + Str4() + "-" + Str4() + "-" + Str4() + Str4() + Str4();
}

//获取URL参数
function Request(paras) {
    var url = location.href;
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof (returnValue) == "undefined") {
        return "";
    } else {
        return returnValue;
    }
}

数组增加IndexOf属性

function CheckIndexOf() {
    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function (elt /*, from*/) {
            var len = this.length >>> 0;

            var from = Number(arguments[1]) || 0;
            from = (from < 0)
                 ? Math.ceil(from)
                 : Math.floor(from);
            if (from < 0)
                from += len;

            for (; from < len; from++) {
                if (from in this &&
                    this[from] === elt)
                    return from;
            }
            return -1;
        };
    }
}

window。Open

screenH = window.screen.availHeight;
screenW = window.screen.availWidth
function openWin(winUrl, cWidth, cHeight) {
    var height = cHeight == null ? 400 : cHeight;
    var width = cWidth == null ? 400 : cWidth;
    var pos_left = (screenW - width) / 2;
    var pos_top = (screenH - height) / 2;
    var winid = Str4();
    window.open(winUrl, winid, 'height=' + height + ', width=' + width + ', top=' + pos_top + ', left=' + pos_left + ', toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, status=no');
}

原文地址:https://www.cnblogs.com/randyzhuwei/p/4019159.html