Get URL Parameters Using Javascript
By Julia Khusainova • November 4, 2009 • Posted in Development, Tutorials • 2 Comments »
Javascript is a great tool to manage client-side presentation. You can feel a power of it when you turn to jQuery, or willing to implement animation or validation, etc. etc. A lot of cool things can be done with Javascript.
Hovewer there are there are some significant techniques that cannot be done that easy with javascript.
One of them is parsing query. It is essential that you know what parameters have come when user submitted the form and handle the request upon this. Good understanding of regular expressions comes handy. We will use the it to parse a query string.
Objective
Get access to the GET parameters been passed to the page by parsing the query string.
Implementation
function getURLParameter(name)
{
var pattern = "[\\?&]"+name+"=([^]*)";
var regex = new RegExp( pattern );
var res = regex.exec( window.location.href );
if (res == null)
return "";
else
return res[1];
}
Conclusion
The function is performed query parsing and return the parameter value is response to parameter name.
2 Responses So Far


























RSS Feed
Email Feed
Good work Julia..
[Reply]
Julia Reply:
December 1st, 2009 at 3:53 am
Thank you Rory
[Reply]