Skip to content

Instantly share code, notes, and snippets.

@and3rson
Last active August 14, 2018 09:35
Show Gist options
  • Save and3rson/fcc1d901e00ecab4865f84e646edc232 to your computer and use it in GitHub Desktop.
Save and3rson/fcc1d901e00ecab4865f84e646edc232 to your computer and use it in GitHub Desktop.
var getParamElements = () => Array.prototype.slice.call(document.querySelectorAll('.parameters div[name="parameter"]'));
var populateArgs = () => {
if (getParamElements().length) {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
getParamElements().forEach(el => {
var nameEl = el.querySelector('[name="name"]');
var valueEl = el.querySelector('[name="value"]');
if (urlParams.hasOwnProperty(nameEl.value)) {
valueEl.value = urlParams[nameEl.value];
}
});
} else {
setTimeout(populateArgs, 250);
}
};
window.addEventListener('ready', populateArgs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment