Last active
February 15, 2016 21:04
-
-
Save dyoung522/6415c94b00ff11737057 to your computer and use it in GitHub Desktop.
JS: Retrieve params from the calling URL
This file contains 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
export default { | |
getParams() { | |
const scripts = document.getElementsByTagName('script'), | |
myScript = scripts[scripts.length - 1], | |
query = myScript.src.replace(/^[^\?]+\??/, ''); | |
let Params = {}; | |
if ( !query ) { return Params } // return empty object | |
const Pairs = query.split(/[;&]/); | |
for ( let i = 0; i < Pairs.length; i++ ) { | |
const KeyVal = Pairs[i].split('='); | |
if ( !KeyVal || KeyVal.length !== 2 ) { continue } | |
const key = decodeURI(KeyVal[0]), | |
val = decodeURI(KeyVal[1]); | |
Params[key] = val.replace(/\+/g, ' '); | |
} | |
return Params; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit to Developing Featherweight Web Services with JavaScript