示例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
}
网友评论