美文网首页
获取url参数并转换成对象

获取url参数并转换成对象

作者: RickyWu585 | 来源:发表于2021-05-16 13:30 被阅读0次

    示例url:localhost:8080?name=mike&age=20

    window.location.search
    // ?name=mike&age=20
    
    function getQueryParams(){
      const result = {}
      const querystring =  window.location.search
      // ?name=mike&age=20
      const reg = /[?&][^?&]+=[^?&]+/g
      const found = querystring.match(reg)
       // ['?name=mike',&age=20]
      if(found){
        found.foreach(item = > {
            let temp = item.substring(1).split('=')
            let key = temp[0]
            let value = temp[1]
            result[key] = value
        })
      }  
      return result
    }
    

    相关文章

      网友评论

          本文标题:获取url参数并转换成对象

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