tags:

views:

571

answers:

6

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 submitting to itself, not a server, so there is no GET request and definitely no CGI program.)

I've seen a number of routines on the net that I can copy, but I have no idea how robust any of them are. I'm used to other languages like Perl and Java where I can rely on an extremely well-tested and robust library that I know will handle millions of little edge-cases in the standard. I would like the same here, rather than just cutting and pasting an example.

+5  A: 

jQuery URL Utils or jQuery URL Parser.

Darin Dimitrov
I wanted to go this route, but it was too much to learn and get working today. It's my preferred answer for anyone with enough competence and who can handle the overhead of the jQuery framework.
skiphoppy
+3  A: 

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

Here is a sample of the 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");
Andreas Grech
Thank you. That was lightweight and minimal yet still accomplished what I wanted: a library, rather than examples to copy. I wanted to use JQuery, but couldn't seem to figure it all out today, so that'll have to wait for another time.
skiphoppy
The reason I posted these instead of jQuery is because you don't need a whole framework to manage such simple tasks.
Andreas Grech
Yes, and today it was nice to not have to rely on the whole framework.
skiphoppy
+1  A: 

If it's "submitting to itself," do you need to do GET parameters?

But if you do, most browsers now have the decodeURIComponent function which will handle individual parameters; your job is to split them on & (String#split will do that nicely). If you want a library, jQuery and Prototype are both well-used and tested.

T.J. Crowder
+1  A: 

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.

Jasconius
If you do that, you'll need to run both the keys and values through `decodeURIComponent`.
T.J. Crowder
Oops, one bug already for not using a library! :)
skiphoppy
A: 

Javascript has no built in support for URL parameters.

Anyway, the location.search property returns the portion of current page URL starting from the question mark ('?').

From this, you can write your own parameter parser or you can make use of one of those available in most common Javascript frameworks, such as JQuery and similar.

egapotz
A: 

I think this library would work quite well, it is independent so you can use it with JQuery or with YAHOO or Dojo, another advantage is that it is pretty 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 of the work for you in this case. It is only like 1.2 KB compressed so you could even include it in a bigger library if you wanted.

jhuni