获取location中search的值

location.href = 'http:www.baidu.com?name=zhang3&age=18'

URL

const searchParams = new URL(location.href).searchParams
const name = searchParams.get('name')
const age = searchParams.get('age')

console.log(name) // zhang3
console.log(age) // 18

URLSearchParams

const searchParams = new URLSearchParams(location.search);

const name = searchParams.get('name')
const age = searchParams.get('age')

console.log(name) // zhang3
console.log(age) // 18
posted @ 2021-12-01 09:42  _Jarrett  阅读(27)  评论(0编辑  收藏  举报