/**
*
* @returns 获取url的参数,拼接成对象
*/
const parseUrl = () => {
var searchHref = window.location.href.split('?')[1];
var params = searchHref?searchHref.split('&'):[];
var returnParam = {};
params.forEach(function(param) {
var paramSplit = param.split('=');
returnParam[paramSplit[0]] = paramSplit[1];
});
return returnParam;//得到一个对象 {id: "1064", td_channelid: "3"}
}
或者:
function deconstructUrl(url='') {
if(!url.includes('?')){
return ;
}
let params = {};
let arr = url.split('?')[1].split('&');
arr.forEach(function(item){
let entries = item.split('=');
params[entries[0]] = decodeURI(entries[1]);
});
return params;
}
或者
getSearch(key,url=window.location .href){
let reg =new RegExp(`[\?|&]{1}${key}=([^&#]*)`,"i");
let valueArr = reg.exec(url)
return valueArr ?valueArr [1]:""
}
key是你想获取到的参数的key,url是你需要解析的地址,默认是浏览器的地址