Skip to content

Instantly share code, notes, and snippets.

@smonteverdi
Created August 23, 2012 19:08
Show Gist options
  • Save smonteverdi/3440448 to your computer and use it in GitHub Desktop.
Save smonteverdi/3440448 to your computer and use it in GitHub Desktop.
jQuery: Get URL Variable
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
// example.com?param1=name&param2=&id=6
$.urlParam('param1'); // name
$.urlParam('id'); // 6
$.urlParam('param2'); // null
//example params with spaces
http://www.jquery4u.com?city=Gold Coast
console.log($.urlParam('city'));
//output: Gold%20Coast
console.log(decodeURIComponent($.urlParam('city')));
//output: Gold Coast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment