js 为url字符串添加、修改参数

 

 

//为url字符串添加、修改参数
String.prototype.EditUrlParam = function (paramName, replaceWith) {
    var url = this;
    if (url.indexOf(paramName) > -1) {
        var re = eval('/(' + paramName + '=)([^&]*)/gi');
        url = url.replace(re, paramName + '=' + replaceWith);
    } else {
        var paraStr = paramName + '=' + replaceWith;

        var idx = url.indexOf('?');
        if (idx < 0)
            url += '?';
        else if (idx >= 0 && idx != url.length - 1)
            url += '&';
        url=url + paraStr;
    }
    return url;
};

 

posted @ 2019-08-05 11:07  三瑞  阅读(2602)  评论(0编辑  收藏  举报