There are two ways to parse the query parameters of a URL into a Dictionary object:
1. Manual parsing
1 functionGetquerystringargs (URL) {2url = URL = =NULL?Window.location.href:url;3 varQS = url.substring (Url.lastindexof ("?") + 1);4 varargs = {};5 varItems = qs.length > 0? Qs.split (' & ')) : [];6 varitem =NULL;7 varName =NULL;8 varValue =NULL;9 for(vari=0; i<items.length; i++){TenItem = items[i].split ("="); One //use decodeURIComponent () to decode name and value separately (because the query string should be encoded). AName = decodeURIComponent (item[0]); -Value = decodeURIComponent (item[1]); - the if(name.length) { -Args[name] =value; - } - } + - returnargs; + } AConsole.log (Getquerystringargs (' Https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=12306%E7%81%AB %e8%bd%a6%e7%a5%a8%e7%bd%91%e4%b8%8a%e8%ae%a2%e7%a5%a8%e5%ae%98%e7%bd%91 ')); at //Object {tn: "MONLINE_3_DG", ie: "Utf-8", WD: "12306 train tickets online booking official website"}
2. Using the regular
1 functiongetqueryobject (URL) {2url = URL = =NULL?Window.location.href:url;3 varSearch = url.substring (Url.lastindexof ("?") + 1);4 varobj = {};5 varreg =/([^?&=]+) = ([^?&=]*)/G;6 //[^?&=]+ says: except? , &, = one or more characters away from7 //[^?&=]* says: except? , &, = 0 to more characters (any number)8Search.replace (Reg,function(RS, $, $) {9 varName = decodeURIComponent ($);Ten varval = decodeuricomponent ($); Oneval =String (val); AObj[name] =Val; - returnrs; - }); the returnobj; - } -Console.log (Getqueryobject (' https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=12306%E7%81%AB%E8% Bd%a6%e7%a5%a8%e7%bd%91%e4%b8%8a%e8%ae%a2%e7%a5%a8%e5%ae%98%e7%bd%91 ')); - //Object {tn: "MONLINE_3_DG", ie: "Utf-8", WD: "12306 train tickets online booking official website"}
Resolves query parameters of a URL to a Dictionary object