Urlsearchparams interface Definition processing URL parameter string

Source: Internet
Author: User

The basic use method is as follows

/** Urlsearchparams Attribute * @ Syntax: new urlsearchparams (parameter);*/(function(){    varstr = "http://www.domain.com/?user=Alan&id=123&id=456"; varSearchparams =Newurlsearchparams (str); //let...of Syntax (Iteration object)     for(let p of Searchparams) {//Console.log (p);    }/** Urlsearchparams.append () * @ Add new key/value to URL*/Searchparams.append ("City", encodeURIComponent ("Shenzhen"));/** Urlsearchparams.has () * @ query is present, returns a Boolean value*/Searchparams.has ("id");//true/** Urlsearchparams.get () * @ Returns the first value associated*/Searchparams.get ("id");//123/** Urlsearchparams.getall () * @ Returns all values of the same key*/Searchparams.getall ("id");//["123", "456"]/** Urlsearchparams.set () * @ Sets the value of key, if there are multiple, delete the other*/Searchparams.set ("id", "789");/** Searchparams.delete () * @ Delete all parameter lists key and value values, duplicate keys are deleted*/Searchparams.Delete("id");/** searchparams.entries () * @ returns all key-value pairs Key/value*/     for(Let pair of searchparams.entries ()) {Console.log (pair[0]+ ', ' + pair[1]); }/** Searchparams.keys () * @ return all keys key*/     for(Let key of Searchparams.keys ()) {Console.log (key); }/** searchparams.values () * @ return all values*/     for(Let value of Searchparams.values ()) {Console.log (value); }/** searchparams.tostring () * @ return the modified URL*/searchparams.tostring (); Console.log (Searchparams.tostring ());} ());

JS write function resolves all parameters from the following URL string

http://www.domain.com/?user=anonymous&id=123&id=456&city=%E5%8C%97%E4%BA%AC&d &enabled

The expected return result format is as follows:

{  ' anonymous ',  ID: [123, 456],     ///  repeat key to assemble an array, which can be converted into a number type City  : ' Beijing ',        // English  true,      //  the key contract value of unspecified value is true}

Make sure you know the problem before you start. How do you figure out the problem? To clearly enter the characteristics, whether there will be a variety of strange input

Defensive programming, detecting if it is a string

functionParse (str) {if(typeofSTR!== "string"){            return; }        varParamobj = {}; vardecode = decodeuricomponent (str);//Decode First        varParsepart = Decode.split ("? #") [Decode.split ("? #"). Length-1];//truncation does not require part        varParamarr = Parsepart.split ("&");  for(vari = 0; i < paramarr.length; i++){            varTMP = paramarr[i].split ("="); varKey = Tmp[0]; varValue = Tmp[1] | |true; //console.log ("keyword is:" + key, "value is:" + value);            if(typeofParamobj[key] = = = "undefined") {Paramobj[key]=value; }Else{                varNewValue = Paramobj[key] + "," + value;//There are multiple duplicate first connection strings before splitting openParamobj[key] = Newvalue.split (","); }        }        //Console.log (paramobj);}

Urlsearchparams interface Definition processing URL parameter string

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.