Skip to content

Instantly share code, notes, and snippets.

@danferth
Last active May 22, 2019 22:52
Show Gist options
  • Select an option

  • Save danferth/5587587 to your computer and use it in GitHub Desktop.

Select an option

Save danferth/5587587 to your computer and use it in GitHub Desktop.
Get URL values with jQuery

Grab the $_GET[] from a url with JS

Cheat sheet for ustilizing the $_GET[] from a URL in JS

Real simple little function will grab the $_GET[] values and put them into an array for use with jQuery. I didn't write it and sorry but do not remember where it came from but man has it been usefull at times.

thanks to the original author!


With all the cheat sheets thay are a work in progress and could contain mistakes. If you find these helpfull or find shit that is just plain wrong, please comment so I can fix.

Thanks

$(document).ready(function(){
// this function will grab the $_GET values from the url and put them into an array
$.extend({
getUrlVars: function(){
var vars = [], hash;
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 $.getUrlVars()[name];
}
});
//this is how you retreive a single value "name"
var byName = $.getUrlVar('name');
//alert (byName);
@danferth

Copy link
Copy Markdown
Author

Get URL values with jQuery

  • Possible to use form with method="GET" and use appended query in jQuery
  • Used on htslabs.com - on product pages form with hidden input
  • user clicks submit and action takes them to tech library
  • On library query string has value of hidden input
  • jQuery used to open product section they were on

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