js 常用的一些函数

  //设置默认焦点
    var setFocus = function SetFocus(elementId) {
        document.onkeydown = function (event_e) {
            if (window.event) event_e = window.event;
            var int_keycode = event_e.charCode || event_e.keyCode;
            if (int_keycode == '13') {
                document.getElementById(elementId).focus();;
            }
        }
    };

  //把url参数转化成一个对象返回
    var readUrlToParams = function () {
        var url = location.href;
        var nameValue;
        var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
        var paraObj = {};
        for (var i = 0; nameValue = paraString[i]; i++) {
            var name = nameValue.substring(0, nameValue.indexOf("=")).toLowerCase();
            var value = nameValue.substring(nameValue.indexOf("=") + 1, nameValue.length);
            if (value.indexOf("#") > -1) {
                value = value.split("#")[0];
            }
            paraObj[name] = value;
        }
        return paraObj;
    };

  var isNullObj = function (obj) {
        if (obj == null || obj == "null") {
            return true;
        }
        return false;
    };
    var isNullOrWhiteSpaceString = function (str) {
        if (str == null || str.fulltrim() == "") {
            return true;
        }
        return false;
    };

//对string的一些扩展
    String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ''); };
    String.prototype.ltrim = function () { return this.replace(/^\s+/, ''); };
    String.prototype.rtrim = function () { return this.replace(/\s+$/, ''); };
    String.prototype.fulltrim = function () { return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '').replace(/\s+/g, ' '); };


    //对array的一些扩展
    Array.prototype.indexOf = function (val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == val) return i;
        }
        return -1;
    };
    Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };
    Array.prototype.contains = function (element) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == element) {
                return true;
            }
        }
        return false;
    };
    Array.prototype.clear = function () {
        var length = this.length;
        this.splice(0, length);
    };//清空数组

你可能感兴趣的:(js)