Last active
February 5, 2018 15:03
-
-
Save freddielore/4248ffed42df6b6e07752696d802e4c3 to your computer and use it in GitHub Desktop.
Get query string from URL via Javascript #javascript
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
// 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