Created
August 23, 2012 19:08
-
-
Save smonteverdi/3440448 to your computer and use it in GitHub Desktop.
jQuery: Get URL Variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.urlParam = function(name){ | |
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); | |
return results[1] || 0; | |
} | |
// example.com?param1=name¶m2=&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