Created
February 25, 2011 20:05
-
-
Save revolunet/844407 to your computer and use it in GitHub Desktop.
send data with POST without physical form (no dependency)
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 buildForm(a,e,c,d){if(!d){d=""}var b="<form id='form-"+d+"' method='"+e+"' action='"+a+"' target='"+d+"'>";b+='<input type="hidden" name="cb" value="alert"/>';for(i in c){if(c.hasOwnProperty(i)){b+='<input type="hidden" name="'+i+'" value="'+c[i]+'"/>'}}b+="</form>";return b}function createIframe(b){var a="<iframe id='"+b+"' name='"+b+"' width=1000 height=400 src='about:blank'></iframe>";document.body.innerHTML+=a}function postForm(a,e,b,c){var d="postForm";if(!c){c="iframe-"+d}if(!document.getElementById(c)){createIframe(c)}f=buildForm(a,e,b,c);if(!document.getElementById("postForm-container")){document.body.innerHTML+="<div id='postForm-container'></div>"}document.getElementById("postForm-container").innerHTML=f;document.getElementById("form-"+c).submit()}; |
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
// Post an array of data to a remote URL without existing physical form | |
// | |
// ex : postForm('http://app.revolunet.com', 'POST', {data:'pouet-'+Math.random()}); | |
// | |
function buildForm(url, method, data, target) { | |
// build raw form | |
if (!target) target=''; | |
var f = "<form id='form-"+target+"' method='" + method + "' action='" + url + "' target='" + target + "'>"; | |
f += '<input type="hidden" name="cb" value="alert"/>'; | |
for (i in data) { | |
if (data.hasOwnProperty(i)) { | |
f += '<input type="hidden" name="'+i+'" value="'+data[i]+'"/>'; | |
} | |
} | |
f += "</form>"; | |
return f; | |
} | |
function createIframe( id ) { | |
// add an iframe to the page | |
var i ="<iframe id='"+id+"' name='"+id+"' width=1000 height=400 src='about:blank'></iframe>"; | |
document.body.innerHTML+=i; | |
} | |
function postForm(url, method, data, target) { | |
// create the iframe, then the form, then submit the form to the iframe | |
var id = 'postForm'; | |
if (!target) target = 'iframe-'+id; | |
if (!document.getElementById(target)) { | |
createIframe( target ); | |
} | |
f = buildForm(url, method, data, target); | |
if (!document.getElementById('postForm-container')) { | |
document.body.innerHTML+="<div id='postForm-container'></div>"; | |
} | |
document.getElementById('postForm-container').innerHTML = f; | |
document.getElementById('form-'+target).submit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment