Created
June 17, 2015 18:56
-
-
Save kmck/26e50ea53f21abf4e0e8 to your computer and use it in GitHub Desktop.
Fake a form posting action
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
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