lambacck
lambacck

Reputation: 9926

Why does Internet Explorer open form submission in a new window and not in a dynamically inserted iframe?

I am trying to get post a form to a hidden, dynamically inserted iframe, but in Internet Explorer the form submission opens in a new window.

var iframe = document.createElement('iframe');
iframe.name = 'hidden_iframe';
iframe.className = 'NotVisible';
iframe.id = 'hidden_iframe';
document.body.appendChild(iframe);

var my_form = document.getElementById('my_form');
my_form.target = 'hidden_iframe';

This works in Firefox but not Internet Explorer.

Upvotes: 5

Views: 4568

Answers (2)

Soviut
Soviut

Reputation: 91585

Could this not be more easily accomplished using an Ajax submit? The iframe approach you're presenting is very hackish and prone to lots of issues.

Here is a jQuery Form plugin that makes doing Ajax submits easy by handling all the serialization of the form values for you.

Upvotes: 0

lambacck
lambacck

Reputation: 9926

Apparently you need to include the name in the call to createElement. This works in IE and causes an exception in standards compliant browsers. We get:

var iframe;
try {
    iframe = document.createElement('<iframe name="hidden_iframe">');
} catch (ex) {
    iframe = document.createElement('iframe');
    iframe.name='hidden_iframe';
}

iframe.className = 'NotVisible';
iframe.id = 'hidden_iframe';
document.body.appendChild(iframe);

var my_form = document.getElementById('my_form');
my_form.target = 'hidden_iframe';

Upvotes: 14

Related Questions