Skip to content

Instantly share code, notes, and snippets.

@tokuhirom
Created January 27, 2015 13:38
Show Gist options
  • Save tokuhirom/cb8d74b57f95c5868a4a to your computer and use it in GitHub Desktop.
Save tokuhirom/cb8d74b57f95c5868a4a to your computer and use it in GitHub Desktop.
fillinform.js
(function () {
// public domain. buggy.
"use strict"
var elems = document.getElementsByClassName("fillinform");
var query = parseQuery();
for (var i = 0, l = elems.length; i < l; i++) {
var it = elems[i];
Object.keys(query).forEach(function (key) {
var input = it.querySelector("input[name=" + key + "]");
if (input.type == "checkbox") {
input.checked = true;
} else {
input.value = query[key];
}
});
}
function parseQuery() {
var query = window.location.search.substring(1);
var vars = query.split('&');
var retval = { };
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
var key = decodeURIComponent(pair[0]);
var val = decodeURIComponent(pair[1]);
retval[key] = val;
}
return retval;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment