獲取瀏覽器url參數(shù)是比較常見的問題考杉,所以我將其封裝成一個(gè)公共函數(shù)來(lái)隨時(shí)調(diào)用
以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"}
}
獲取時(shí)調(diào)用getUrlParams方法
var obj = getUrlParams();
obj.ie//"UTF-8"
obj.wd//"object"