Skip to content

Instantly share code, notes, and snippets.

@jgoslow
Last active August 13, 2022 18:13
Show Gist options
  • Save jgoslow/c6d91c96eed0360e46414be18ccf6ab8 to your computer and use it in GitHub Desktop.
Save jgoslow/c6d91c96eed0360e46414be18ccf6ab8 to your computer and use it in GitHub Desktop.
URL javascript functions
function formatParamsObjectToURL( params ){
return "?" + Object
.keys(params)
.map(function(key){
return key+"="+encodeURIComponent(params[key])
})
.join("&")
}
/**
* Returns object of URL params
*/
function getQueryParams(qs) {
qs = qs.split('+').join(' ');
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment