Created
October 27, 2014 21:30
-
-
Save lionelB/bc60ac82eeded37b43ad to your computer and use it in GitHub Desktop.
extract url params
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
//Since http://devdocs.io/dom/urlutils.searchparams is not already bake! | |
//the url to parse | |
var url = "https://toto.com/truc/?param1=yo¶m2=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