获取url上的参数
程序员文章站
2022-06-24 15:26:15
var aa = '?name=hss&age=13'; function strToObj(str){ if(typeof str === 'undefined'){ str = location.search; } var reg = /[\?|&]+(\w+)=(\w*)/g; ... ......
var aa = '?name=hss&age=13';
function strtoobj(str){
if(typeof str === 'undefined'){
str = location.search;
}
var reg = /[\?|&]+(\w+)=(\w*)/g;
var param = {};
do{
var item = reg.exec(str);
if(item !== null){
param[item[1]] = item[2]
}
}while(reg.lastindex > 0)
return param;
}
console.time()
console.log(strtoobj());
console.timeend()
执行效率还是蛮高的大概在一毫秒吧