location.search 进行处理,返回包含所有参数的一个对象

 const qs = location.search.length > 0 ? location.search.substr(1) : '';
 // 去掉?
 let args = {
    
    };
 const itemsArr = qs.length > 0 ? qs.split('&') : [];
 itemsArr.forEach((el) => {
    
    
   let item = el.split('=');
   let name = decodeURIComponent(item[0]);
   let value = decodeURIComponent(item[1]);
   if( name.length ){
    
    
     args[name] = value;
   }
 })
 return args;

例如url
http://xxxx:8020/suzhou/810.html?type=main&id=001&key=jsdf
结果

{
    
    
	type: main,
	id: 001,
	key: jsdf,
}

但是这个方法,search部分不能出现相同的字段俩次。

猜你喜欢

转载自blog.csdn.net/Beth__hui/article/details/109247925
今日推荐