Skip to content

url参数提取

js
// url提取
function urlSearch(url) {
    let query = url.split('?').pop()
    let params = query.split('&')
    let res = {}
    params.forEach(item => {
        let key = item.split('=')[0]
        let value = item.split('=')[1]
        res[key] = value
    })
    console.log(res)
    return res
}
let url = 'http://baidu.com?name=chaos&age=18&sex='
// urlSearch(url)
let query = new URLSearchParams(url.split('?').pop())
console.log(query)