Skip to content

Instantly share code, notes, and snippets.

@Morasta
Created October 22, 2014 19:02
Show Gist options
  • Save Morasta/658af9b558a508feee7b to your computer and use it in GitHub Desktop.
Save Morasta/658af9b558a508feee7b to your computer and use it in GitHub Desktop.
jquery getUrlVars extension
//adapted from http://stackoverflow.com/questions/6001839/check-whether-a-url-variable-is-set-using-jquery
//modified to return empty array when no ? params are set
$.extend({
getUrlVars: function(){ //return all vars set in $_GET
var vars = [], hash;
if (window.location.href.indexOf('?') != -1) {
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
}
return vars;
},
getUrlVar: function(name){ //return single var value by name
return $.getUrlVars()[name];
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment