Skip to content

Instantly share code, notes, and snippets.

@iruslani
Created May 9, 2012 22:30
Show Gist options
  • Save iruslani/2649389 to your computer and use it in GitHub Desktop.
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.
<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&param2=&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>
@iruslani
Copy link
Author

iruslani commented May 9, 2012

Updated to use jquery version and include some example alerts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment