View Full Version : jQuery.url - easy URL parameter retrieval

11-24-2012, 01:16 AM
This is a simple jQuery plugin to pull parameters from a URL; if not available, will return 'undefined'

For example, if the page is http://example.com/index.html?foo=bar, $.url("foo"); will return bar

jQuery.url = function(paramName) {
var results = new RegExp("[\\?&]"+paramName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]")+"=([^&#]*)").exec( window.location.href );
if(results == null){return "undefined";} else{return results[1];}

Not sure how useful this really is, but I hope it helps somebody. :D

Alex Vincent
11-24-2012, 01:52 AM
Sounds interesting. A couple observations:

I learned a while ago that else after return is not needed.
Your check for results == null is also a little off. You could simply write !results .