Skip to content

Instantly share code, notes, and snippets.

@kmck
Created June 17, 2015 18:56
Show Gist options
  • Save kmck/26e50ea53f21abf4e0e8 to your computer and use it in GitHub Desktop.
Save kmck/26e50ea53f21abf4e0e8 to your computer and use it in GitHub Desktop.
Fake a form posting action
function postForm(action, data) {
var form = document.createElement('form');
form.method = 'POST';
form.action = action;
var key, value, input;
for (key in data) {
value = data[key];
if (value.indexOf('\n') < 0) {
input = document.createElement('input');
input.name = key;
input.setAttribute('value', value);
} else {
input = document.createElement('textarea');
input.name = key;
input.innerHTML = value;
}
form.appendChild(input);
}
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
return form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment