Skip to content

Instantly share code, notes, and snippets.

@dyoung522
Last active February 15, 2016 21:04
Show Gist options
  • Save dyoung522/6415c94b00ff11737057 to your computer and use it in GitHub Desktop.
Save dyoung522/6415c94b00ff11737057 to your computer and use it in GitHub Desktop.
JS: Retrieve params from the calling URL
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;
}
};
@dyoung522
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment