Get URL Parameters Using Javascript

By Julia Khusainova • November 4, 2009 • Posted in Development, Tutorials2 Comments »

  • Google Buzz

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.

No related posts.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses

November 29, 2009
Rory Dredhart

Good work Julia..

[Reply]

Julia Reply:

Thank you Rory

[Reply]