Created
August 7, 2016 11:12
-
-
Save zhang-ning/a7af5d06d9cf3022761c6ca3e512deb1 to your computer and use it in GitHub Desktop.
sending form to hiiden iframe
This file contains hidden or 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
//copyed from https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_forms_through_JavaScript | |
// Let's create the iFrame used to send our data | |
var iframe = document.createElement("iframe"); | |
iframe.name = "myTarget"; | |
// Next, attach the iFrame to the main document | |
window.addEventListener("load", function () { | |
iframe.style.display = "none"; | |
document.body.appendChild(iframe); | |
}); | |
// This is the function used to actually send the data | |
// It takes one parameter, which is an object populated with key/value pairs. | |
function sendData(data) { | |
var name, | |
form = document.createElement("form"), | |
node = document.createElement("input"); | |
// Define what should happen when the response is loaded | |
iframe.addEventListener("load", function () { | |
alert("Yeah! Data sent."); | |
}); | |
form.action = "http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi"; | |
form.target = iframe.name; | |
for(name in data) { | |
node.name = name; | |
node.value = data[name].toString(); | |
form.appendChild(node.cloneNode()); | |
} | |
// To be sent, the form needs to be attached to the main document. | |
form.style.display = "none"; | |
document.body.appendChild(form); | |
form.submit(); | |
// But once the form is sent, it's useless to keep it. | |
document.body.removeChild(form); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment