Resolves query parameters of a URL to a Dictionary object

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.