Created
May 9, 2012 22:30
-
-
Save iruslani/2649389 to your computer and use it in GitHub Desktop.
Simple Javascript function to grab the parameter in the url and send an alert.
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
<script type="text/javascript"> | |
$(document).ready(function(){ | |
$.urlParam = function(name){ | |
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); | |
return results[1] || 0; | |
} | |
// example.com?param1=name¶m2=&id=6 | |
alert($.urlParam('param1')); // name | |
alert($.urlParam('question')); // name | |
alert(decodeURIComponent($.urlParam('question'))); | |
$.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 | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use jquery version and include some example alerts.