JS 公共方法

1、Format函数

$.GM.Format = function (source, params) {
        if (arguments.length == 1)
            return function () {
                var args = $.makeArray(arguments);
                args.unshift(source);
                return $.format.apply(this, args);
            };
        if (arguments.length > 2 && params.constructor != Array) {
            params = $.makeArray(arguments).slice(1);
        }
        if (params.constructor != Array) {
            params = [params];
        }
        $.each(params, function (i, n) {
            source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
        });
        return source;
    }

 

调用:

var strTableTemplate = "<td>{0}-{1}-{2}</td>"
 var strTemplate = $.GM.Format(strTableTemplate, str1,str2,str3);

 

2、GenerateGuid函数(生成随即GUID)

 $.GM.NewGuid = function () {
        var guid = "";
        for (var i = 1; i <= 32; i++) {
            var n = Math.floor(Math.random() * 16.0).toString(16);
            guid += n;
            if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                guid += "-";
        }
        return guid;
    };

 

3、获取Url里面的get提交参数

 

   $.Gmfp.GetUrlParams = function () {
        var strRawUrl = decodeURI(window.location);
        var regex = /^[^&]+?[?]([\s\S]*)$/g;
        var matches = regex.exec(strRawUrl);
        if (matches && matches.length >= 2) {
            var strParams = matches[1];
            regex = /([^&=]+)=([^&=]*)/g;
            var resObj = {};
            while (matches = regex.exec(strParams)) {
                resObj[matches[1]] = matches[2];
            }
            return resObj;
        }
        else {
            return {};
        }
    }

 

posted @ 2014-05-16 18:25    阅读(646)  评论(0编辑  收藏  举报