美文网首页
获取地址栏中的参数

获取地址栏中的参数

作者: 无缘霸哥 | 来源:发表于2020-04-16 14:16 被阅读0次
    const getQueryString = (str) => {
        if (!str) {
          return null;
        }
        let array = str.slice(str.indexOf('?') + 1).split('&');
        let obj = {};
        array.forEach((item) => {
          let index = item.indexOf('=');
          obj[item.slice(0, index)] = item.slice(index + 1);
        });
        return obj;
      };
    

    自己模拟地址测试:

    const url = 'index.html?type=add&id=1234';
    console.log(getQueryString(url)) ;
    console.log的输出结果:
    {type: "add", id: "1234"}
    

    相关文章

      网友评论

          本文标题:获取地址栏中的参数

          本文链接:https://www.haomeiwen.com/subject/iaurvhtx.html