Tim Jansen
Tim Jansen

Reputation: 3368

Submit Form into specific window

I have a HTML form in a popup window's iframe and would like to submit it into the popup's opening window (window.parent.opener). The form does not exist in the opening window, but only in the popup's iframe. What's the easiest way to do this?

I figure form targets don't help, as they only allow to specify _parent, and the opening window does not have an iframename. Dynamically re-creating the iframe's form in the opening window could be an option, as long as it does not become visible. Is there a library to dynamically create a form given serialized form data?

(jQuery solutions preferred)

Upvotes: 0

Views: 718

Answers (2)

Tim Jansen
Tim Jansen

Reputation: 3368

Dynamically re-creating the form in the other window turned out to be not as bad as I thought. This snippet should do it:

var originalForm = $('#myForm');
var values = originalForm.serializeArray();
var newForm = $('<form>').attr({method: 'post', action: originalForm.prop('action')});
$.each(values, function(i, val) {
    newForm.append($('<input>').attr({type: 'hidden'}).attr(val));
});
$('body', window.parent.opener.document).append(newForm);
newForm.submit();

Upvotes: 2

Romeo
Romeo

Reputation: 531

Can't you serialize all the data from the form and pass it to the parent via window.parent.opener ?

Or use the URL with something like window.parent.opener.location = "http://something.com?var1=test&var2=test".

For POST, try dinamically setting the target:

document.whateverForm.target = window.parent.opener.window.name;

Upvotes: 1

Related Questions