JS获取浏览器url的参数
2020-03-31 本文已影响0人
活泼lee
获取浏览器url参数是比较常见的问题,所以我将其封装成一个公共函数来随时调用
以https://www.baidu.com/s?ie=UTF-8&wd=object 为例
function getUrlParams(){//location.search-->?ie=UTF-8&wd=object
var arr = location.search.slice(1).split('&') //["ie=UTF-8", "wd=object"]
var obj = {}
arr.map(item=>{
obj[item.split("=")[0]] = item.split("=")[1]
})
return obj //{ie: "UTF-8", wd: "object"}
}
获取时调用getUrlParams方法
var obj = getUrlParams();
obj.ie//"UTF-8"
obj.wd//"object"