js一些常用方法

來源:互聯網
上載者:User

標籤:style   color   io   os   ar   for   sp   on   cti   

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‘);
}

js一些常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.