js给url字符串添加参数

changeZhiboUrl(url){
    //转换跳转直播的url
      let str1 = this.addOrgToUrl(url,'u_code',this.getLoginAfterData.id);
      let str2 = this.addOrgToUrl(str1,'authRight',localStorage.getItem('meetingManager_authRight'));
      return str2;
    },
    addOrgToUrl(url,paramName,replaceWith){
    //url字符串添加参数
    //url:路径地址 paramName:参数名 replaceWith:参数值
      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;
    },

猜你喜欢

转载自www.cnblogs.com/fqh123/p/12980206.html