Created
May 13, 2020 14:47
-
-
Save phamquocbuu/444a43b9d7ad2fa705e0dbcea8182681 to your computer and use it in GitHub Desktop.
Getting all query string values from a URL with vanilla JavaScript - https://gomakethings.com/getting-all-query-string-values-from-a-url-with-vanilla-js/
This file contains hidden or 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
// Get parameters from the current URL | |
getParams(window.location.href); | |
// Get parameters from a URL string | |
var url = 'https://gomakethings.com?sandwhich=chicken%20salad&bread=wheat'; | |
getParams(url); |
This file contains hidden or 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
var getParams = function (url) { | |
var params = {}; | |
var parser = document.createElement('a'); | |
parser.href = url; | |
var query = parser.search.substring(1); | |
var vars = query.split('&'); | |
for (var i = 0; i < vars.length; i++) { | |
var pair = vars[i].split('='); | |
params[pair[0]] = decodeURIComponent(pair[1]); | |
} | |
return params; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment