Skip to content

Instantly share code, notes, and snippets.

@freddielore
Last active February 5, 2018 15:03
Show Gist options
  • Save freddielore/4248ffed42df6b6e07752696d802e4c3 to your computer and use it in GitHub Desktop.
Save freddielore/4248ffed42df6b6e07752696d802e4c3 to your computer and use it in GitHub Desktop.
Get query string from URL via Javascript #javascript
// e.g URL: https://google.com/?id=foo
// getQueryString('id') // retruns "foo"
getQueryString: function(field, url){
var href = url ? url : window.location.href;
var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
var string = reg.exec(href);
return string ? string[1] : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment