Topic description
Get the parameters in the url
1. Specify the parameter name, return the value of the parameter or an empty string
2. If the parameter name is not specified, return all parameter objects or {}
3. If there are multiple parameters with the same name, return an array
1 function getUrlParam(sUrl, sKey) {
2 var param = sUrl.split('#')[0].split('?')[1 ];
3 if (sKey){ // specify the parameter name
4 var strs = param.split('&' );
5 var arrs = new Array(); // If there are multiple parameters with the same name, return array
6 for ( var i = 0, len = strs.length; i < len; i++ ) {
7 var tmp = strs[i].split('=' );
8 if (tmp[0] == sKey){
9 arrs.push(tmp[1 ]);
10 }
11 }
12 if (arrs.length == 1){ // return the value of the parameter or an empty string
13 return arrs[0 ];
14 } else if (arrs.length == 0 ){
15 return "" ;
16 } else {
17 return arrs;
18 }
19 } else { // Do not specify the parameter name, return all parameter objects or {}
20 if (param == undefined || param == "" ){
21 return {};
22 } else {
23 var strs = param.split('&');
24 var arrObj = new Object();
25 for(var i = 0, len = strs.length; i < len; i++){
26 var tmp = strs[i].split('=');
27 if (!(tmp[0] in arrObj)) {
28 arrObj[tmp[0]] = [];
29 }
30 arrObj[tmp[0]].push(tmp[1]);
31 }
32 return arrObj;
33 }
34 }
35 }
Link: https://www.nowcoder.com/questionTerminal/a3ded747e3884a3c86d09d88d1652e10
Source : Niuke.com