使用js获取页面参数

方法一

function GetUrlParam (name) { 
     return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null 
}

  

方法二

function getUrlParam(paramName) {
      const url = document.location.toString()
      const arrObj = url.split("?")
      if(arrObj.length > 1) {
        const arrPara = arrObj[1].split("&");
        for (var i = 0; i < arrPara.length; i++) {
          const arr = arrPara[i].split("=");
          if (arr != null && arr[0] == paramName) {
            return arr[1];
          }
        }
        return ""
      }
      else {
        return ""
      }
}

  

posted @ 2018-05-25 12:37  想旅游咯  阅读(1284)  评论(0编辑  收藏  举报