function getUrlParam(sUrl, sKey) {
let sParam = sUrl.split('?')[1].split('#')[0]; // 获取到含有地址的字符串
let pArr = sParam.split('&'); // 分解字符串
if (sKey) { // 筛选查找
var newArr = [];
pArr.forEach(function (elm, ind) {
if (elm.split('=')[0] == sKey) { // 满足条件添加进新数组
newArr.push(elm.split('=')[1]);
}
});
if (newArr.length == 1) {
return newArr[0]; // 单个数据返回字符串
}else if (newArr.length == 0) {
return ""; // 不满足条件返回空字符串
}else {
return newArr; // 返回新数组
}
} else { // 无筛选条件
if (!sParam) { // 字符串为空返回空对象
return {};
}else {
var newObj = {};
pArr.forEach(function (elm, ind) {
if (!(elm.split('=')[0] in newObj)) {
newObj[elm.split('=')[0]] = []
}
newObj[elm.split('=')[0]].push(elm.split('=')[1]) // 满足条件添加到对应的键值对中
});
return newObj // 返回新对象
}
};
}