What JavaScript library should I use for parsing URL parameters?

前端 未结 8 1613
终归单人心
终归单人心 2020-12-30 09:52

How do I parse URL parameters in JavaScript? (These are the parameters I would ordinarily call GET parameters or CGI parameters, but in this case the page is basically subm

8条回答
  •  清歌不尽
    2020-12-30 10:40

    The best way I have found is to simply do it yourself and funnel the params into a global key/value object.

    Getting quer params is simple...

    just take a couple of .split()'s

    var myquery = thewholeurl.split("?")[1]; //will get the whole querystring with the ?
    

    then you can do a

    myparams = myquery.split("&")

    then you can do

    for each param in myparams
    {
    temp = param.split("=");
    
    mykeys.push(temp[0]);
    myvalues.push(temp[1]);
    
    OR
    
    myObject[temp[0]] = temp[1];
    }
    

    It's just a matter of style.

    This is not perfect code, just psuedo stuff to give you the idea.

提交回复
热议问题