better2021 / Blog

个人博客
https://feiyuweb.me/Blog
1 stars 0 forks source link

url地址上的参数转换为一个obj对象 #33

Open better2021 opened 4 years ago

better2021 commented 4 years ago
function getUrlParams(url) {
  var search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, ' ') +
      '"}'
  )
}

// var  url = search?q=dsd&oq=dsd&aqs=chrome..69i57j0l7.642j0j15&sourceid=chrome&ie=UTF-8
// JSON.stringify(getUrlParams(url ))
// "{"q":"dsd","oq":"dsd","aqs":"chrome..69i57j0l7.642j0j15","sourceid":"chrome","ie":"UTF-8"}"