Which JavaScript library should I use to parse URL parameters?

How to parse URL parameters in JavaScript? (These are parameters that I would usually call GET or CGI parameters, but in this case the page basically passes itself, not the server, so there is no GET request and, of course, no CGI program.)

I have seen several routines on the network that I can copy, but I have no idea how reliable they are. I am used to other languages ​​such as Perl and Java, where I can rely on a very well-tested and reliable library, which, as I know, will handle millions of small regional cases in the standard. I would like to here, and not just cut and paste an example.

+7
javascript
source share
8 answers

Here are two simple functions that do the job: http://adamv.com/dev/javascript/querystring

Here is an example API reference:

var qs = new Querystring(); // Parse a given querystring var qs2 = new Querystring("name1=value1&name2=value2"); var v1 = qs2.get("name1"); var v3 = qs2.get("name3", "default value"); 
+1
source share
+7
source share

If it "submits to itself", do you need to do GET parameters?

But if you do, most browsers now have a decodeURIComponent function that will handle individual parameters; your job is to break them and ( String#split will do it nicely). If you need a library, jQuery and Prototype are well used and tested.

+1
source share

The best way I've found is to just do it myself and direct the params to a global key / value object.

Getting quer options is just ...

just take a pair of .split () '

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

then you can do

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 is just a matter of style.

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

+1
source share

I am using the parseUri library available here: http://stevenlevithan.com/demo/parseuri/js/

This allows you to do exactly what you ask:

 var uri = 'http://google.com/?q=stackoverflow'; var q = uri.queryKey['q']; // q = 'stackoverflow' 

I used it for a while and did not have any problems.

+1
source share

I found this useful for simple URL analysis by changing the URL (e.g. adding new request parameters): https://github.com/derek-watson/jsUri

+1
source share

I think this library will work very well, it is independent, so you can use it with JQuery or with YAHOO or Dojo, another advantage is that it is well documented.

 http://www.openjsan.org/doc/t/th/theory/HTTP/Query/0.03/lib/HTTP/Query.html 

You can use HTTP.Query to do all the work for you in this case. This is just 1.2 KB compression, so you can even include it in a large library if you want.

0
source share

Javascript does not have built-in support for URL parameters.

In either case, the location.search property returns part of the current page URL, starting with a question mark ('?').

From this, you can write your own parameter parser, or you can use one of the most common Javascript frameworks available, such as jQuery, etc.

-one
source share

All Articles