Skip to content

Instantly share code, notes, and snippets.

@lacymorrow
Last active September 4, 2018 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lacymorrow/19e41994a5b213d4f1200b72878ec798 to your computer and use it in GitHub Desktop.
Save lacymorrow/19e41994a5b213d4f1200b72878ec798 to your computer and use it in GitHub Desktop.
JS - Parse URL `GET` parameters from and HTTP request
/* function parseURLParams
* Accepts a http parameter string or grabs the one from the current URL
* Returns a dictionary
* Ex: '?api_key=XYZ&version=2' => { api_key: 'XYZ', version: 2}
*
* query: string | undefined
*/
function parseURLParams (query) {
if(!query)
query = location.search;
var queryDict = {};
query.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]})
return queryDict;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment