js获取浏览器地址栏参数

有时候我们需要获取浏览器地址栏参数,比如这个地址https://editor.csdn.net/md?not_checkout=1&spm=1001.2101.3001.5352&articleId=131378279 我们想获取它问号后的参数可以调用如下方法:

 var GetRequest = function() {
    
    
      const tokenStr = location.href.split("?")[1];
      if (!tokenStr) {
    
    
        return false;
      }
      const tokenList = tokenStr.split("&");
      const obj = {
    
    };
      tokenList.forEach(item => {
    
    
        obj[item.split("=")[0]] = item.split("=")[1];
      });

      console.log(obj); // {articleId: "131378279",not_checkout:"1",spm: "1001.2101.3001.5352"  }

      return obj;
    }

GetRequest()

猜你喜欢

转载自blog.csdn.net/qq_37635012/article/details/131378279