黑马旅游网 解析url查询字符串
1 function getUrlParam(name) { 2 let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 3 let r = window.location.search.substr(1).match(reg); 4 return r != null ? r[2] : null; 5 } 6 7 function getQueryArgs(){ 8 let qs=location.search.length ? location.search.substring(1) : ""; 9 let args={}; 10 let items=qs.length ? qs.split("&") : []; 11 12 for(let item of items){ 13 let pair=item.split("="); 14 if(pair[0].length){ 15 args[decodeURIComponent(pair[0])]= decodeURIComponent(pair[1]); 16 } 17 } 18 return args; 19 } 20 21 function getQueryStringArgs(){ 22 var qs=location.search.length ? location.search.substring(1) : ""; 23 var args={}; 24 var items=qs.length ? qs.split("&") : []; 25 var item=null, 26 name=null, 27 value=null, 28 len=items.length; 29 for(var i=0;i<len;i++){ 30 item=items[i].split("="); 31 if(item[0].length){ 32 name=decodeURIComponent(item[0]); 33 value=decodeURIComponent(item[1]); 34 args[name]=value; 35 } 36 } 37 return args; 38 } 39 40 function getQuery() { 41 let search = location.search; 42 // alert(search); 43 44 //没有查询参数 45 let i = search.indexOf("?"); 46 if( i == -1){ 47 return {}; 48 }; 49 50 //去除问号 51 search = search.substring(i + 1); 52 53 //得到参数 54 let result = {}; 55 let items = search.split("&"); 56 for(let item of items){ 57 let split = item.split("="); 58 // alert(split[0]); 59 // alert(split[1]); 60 if(split[0]){ 61 result[decodeURIComponent(split[0])] = decodeURIComponent(split[1]); 62 } 63 } 64 return result; 65 }