Skip to content

Instantly share code, notes, and snippets.

@lionelB
Created October 27, 2014 21:30
Show Gist options
  • Save lionelB/bc60ac82eeded37b43ad to your computer and use it in GitHub Desktop.
Save lionelB/bc60ac82eeded37b43ad to your computer and use it in GitHub Desktop.
extract url params
//Since http://devdocs.io/dom/urlutils.searchparams is not already bake!
//the url to parse
var url = "https://toto.com/truc/?param1=yo&param2=toto&tutu=titi";
// Create a HTMLAnchorElement
var a = document.createElement('a');
// Lazy dev let the browser do the hard job and parse the url
a.href = url;
var param = a.search.match(/(?:[\?|&])([^=]+)=([^&]+)/g)
.map(function(pattern){
return pattern.replace(/^\?|&/,'').split('=');
})
.reduce(function(memo, token){
memo[token[0]] = unescape(token[1]);
return memo;
},{});
console.log(param) // Object { param1: "yo", param2: "toto", tutu: "titi" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment