JS分解URL获取所传参数的值
- 将URL地址进行分割
function url_data(url){//URL地址的解析
var obj = {};
if (url.includes('&')) {
let arr1 = url.split('?')[1].split('&');
arr1.forEach(item => {
let arr2 = item.split('=');
let arr_one = arr2[0];
let arr_two = arr2[1];
obj[arr_one] = arr_two;
})
} else {
let arr1 = url.split('?')[1].split('=');
arr1.forEach(item => {
let arr_one = arr1[0];
let arr_two = arr1[1];
obj[arr_one] = arr_two;
})
}
return obj;
}
- 调用方法
var href = window.location.href;
var sobj=url_data(href)
var id = sobj.id;