// 获取url参数functiongetUrlKey(name){return(decodeURIComponent((newRegExp("[?|&]"+ name +"="+"([^&;]+?)(&|#|;|$)").exec(location.href)||[,""])[1].replace(/\+/g,"%20"))||null);},//调用方法getUrlKey("token")
用decodeURIComponent解码,直接返回对象
functiongetParams(url){const res ={}if(url.includes('?')){const str = url.split('?')[1]const arr = str.split('&')
arr.forEach(item=>{const key = item.split('=')[0]const val = item.split('=')[1]
res[key]=decodeURIComponent(val)// 解码})}return res
}// 测试const user =getParams('http://www.baidu.com?user=%E9%98%BF%E9%A3%9E&age=16')
console.log(user)// { user: '阿飞', age: '16' }