Get URL Parameters Using Javascript

By Julia Khusainova • November 4, 2009 • Posted in Development, Tutorials2 Comments »
Get URL Parameters Using Javascript

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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Design Float
  • LinkedIn
  • Mixx
  • NewsVine
  • RSS
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
  • Google Bookmarks

RSS Enjoy this Post? Subscribe to Julia Khusainova's Blog.
RSS Feed   RSS Feed     Email Feed  Email Feed

2 Responses So Far

Rory Dredhart
November 29, 2009

Good work Julia..

[Reply]

Julia Reply:

Thank you Rory

[Reply]


  • CommentLuv Enabled