Javascript to parse query string variables from URL
This is helpful. Get Query String variables in JavaScript :
<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script>Now make a request to page.html?x=Hello
<script>
alert( getQueryVariable("x") );
</script>
Technorati Tags: javascript, code examples, query string

Brilliant!
This just saved me an hour or two.
Thanks!
Posted by: HomeSlice | May 18, 2007 at 06:14 PM
Thanks, worked perfectly also saving me a couple of hours as well. :)
Posted by: pyth0n | Jul 18, 2007 at 11:46 AM
Nice work! Thanks! :) But in my case i needed some case-indifferent parsing. This is easily be added by changing "pair[0] == variable" to "pair[0].toLowerCase()== variable.toLowerCase()" ... Cheers, Stefan
Posted by: Stefan | Aug 08, 2007 at 04:40 AM
This is very helpful.
How about passing multi-word strings as a variable?
Is there a simple syntax to insert spaces?
Thanks
Posted by: jeff | Aug 24, 2007 at 11:49 AM
As an ardent but fledgling javascript hacker, thank you
Al
Posted by: Al | Nov 21, 2007 at 08:37 AM
Cheers, Perfect, so much easier to use than others!
Posted by: Dan | Jan 16, 2008 at 02:45 AM
Stefan, i personally don't recommend using multi-word variables in javascript.
you can use underscores (e.g. 'my_var') or camel-case (eg. myVar)
Posted by: Matthias | Mar 19, 2008 at 07:48 PM
great script, glad i was able to find it. it has helped to some degree
i am running into an error with it and i'm confused as to why, perhaps you could enlighten me. first, i was running into a "XML Parsing Error: not well-formed" in FireFox on the ampersand in the split() - only to realize that it was necessary to escape the & and use &. now i'm running into a "XML Parsing Error: not well-formed" message in the for loop, which has be boggled
Posted by: T | Jul 03, 2008 at 06:05 PM
Here is a blog post describing a function to parse querystring from url usign javascript:
http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx
Posted by: blogging developer | Jul 15, 2008 at 01:01 AM
good one ...
it really help us a lot ...
it almost save a day for us ..
Thanks !
Posted by: Tejas | Sep 10, 2008 at 10:35 PM
perfect, thanks a lot!
Posted by: k | Nov 12, 2008 at 09:27 AM
Thanks, works like a charm. Easy to understand/modify.
Posted by: Aaron Barker | May 13, 2009 at 09:26 AM
as usual, try it in internet explorer first. window.location.search.substring(1) does't work in ie6.
Posted by: ie | Aug 26, 2009 at 06:15 PM
this can really easily be modified to work with iframe sources if you need to parse from those as well.
great resource, I'm working it into my own site with some minor tweaks.
Posted by: anonymouse | Sep 19, 2009 at 09:52 PM
Saved me Two hours at least! Thanks!
Posted by: Dhairyaguptha | May 12, 2010 at 03:03 AM
thnxx...
Posted by: N i T ! N | Jul 10, 2010 at 04:40 AM