Insertion of a targeted IFRAME under IE

C

Csaba Gabor

Under IE 6, if I try to insert an IFRAME by DOM methods,
which IFRAME is targeted by a form, the form won't target
the inserted IFRAME element. Specifically, neither
myFrame.name nor myFrame.setAttribute("name",...)
seems to work. I can bypass the problem by putting in
a DIV and then setting the innerHTML of the DIV to
"<IFRAME name=... >target IFRAME</IFRAME>"
but this is really not very nice.

If there a more proper DOM oriented way to go about this?

Thanks,
Csaba Gabor from Vienna

The below works as expected in FF 1.5, but not in IE 6:
<html>
<head><title>Target test</title></head>
<body onLoad='loaded()'>
<form method=get target=myFrame action="http://google.com">
<button type=submit accessKey=u>S<u>u</u>bmit</button>
</form>

<script type='text/javascript'>
function loaded() {
var myFrame = document.createElement("IFRAME");
myFrame.style.width = '100%';
myFrame.style.height = '7cm';
myFrame.id = 'myFrame';
myFrame.name = 'myFrame';
document.body.appendChild(myFrame); }
</script>
</body>
</html>
 
M

Martin Honnen

Csaba said:
Under IE 6, if I try to insert an IFRAME by DOM methods,
which IFRAME is targeted by a form, the form won't target
the inserted IFRAME element. Specifically, neither
myFrame.name nor myFrame.setAttribute("name",...)
seems to work. I can bypass the problem by putting in
a DIV and then setting the innerHTML of the DIV to
"<IFRAME name=... >target IFRAME</IFRAME>"
but this is really not very nice.

If there a more proper DOM oriented way to go about this?

I don't think so. You need IE specific approaches such as using
innerHTML as you have already found or passing in the attribute in the
createElement method e.g.
var myFrame = document.createElement('<iframe name="myFrame">');
See the documentation:
http://msdn.microsoft.com/en-us/library/ms534184(VS.85).aspx

As the latter does not work outside of IE I would go with creating the
iframe by setting the innerHTML of another element and then extract its
child node.
 
T

Thomas 'PointedEars' Lahn

Csaba said:
Under IE 6, if I try to insert an IFRAME by DOM methods,
which IFRAME is targeted by a form, the form won't target
the inserted IFRAME element. Specifically, neither
myFrame.name nor myFrame.setAttribute("name",...)
seems to work.

Because you are accessing the element object, not the corresponding Window
object.
I can bypass the problem by putting in
a DIV and then setting the innerHTML of the DIV to
"<IFRAME name=... >target IFRAME</IFRAME>"
but this is really not very nice.

If there a more proper DOM oriented way to go about this?

You are using the DOM already, just not the W3C DOM.
[...]
The below works as expected in FF 1.5, but not in IE 6:

Well, it's not Valid to begin with.
<html>
<head><title>Target test</title></head>
<body onLoad='loaded()'>
<form method=get target=myFrame action="http://google.com">

method=get is superfluous. Should be target="myFrame".
<button type=submit accessKey=u>S<u>u</u>bmit</button>
</form>

<script type='text/javascript'>
function loaded() {
var myFrame = document.createElement("IFRAME");

Mapping the case before the call like here might indeed be a good idea.
myFrame.style.width = '100%';
myFrame.style.height = '7cm';

You can't be serious.
myFrame.id = 'myFrame';
myFrame.name = 'myFrame';
document.body.appendChild(myFrame);

And then (as thoroughly explained in an article of Richard Cornford, IIRC):

window.frames["myFrame"].name = "myFrame";

Case closed.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Martin said:
Csaba said:
Under IE 6, if I try to insert an IFRAME by DOM methods,
which IFRAME is targeted by a form, the form won't target
the inserted IFRAME element. Specifically, neither
myFrame.name nor myFrame.setAttribute("name",...)
seems to work. I can bypass the problem by putting in
a DIV and then setting the innerHTML of the DIV to
"<IFRAME name=... >target IFRAME</IFRAME>"
but this is really not very nice.

If there a more proper DOM oriented way to go about this?

I don't think so. You need IE specific approaches such as [...]

No, you don't. In fact, the MSHTML quirk is easily detectable and
fixable; one does not even need to resort to browser sniffing:

var s = "foo";
var f = window.frames;
if (f.name != s) f.name = s;


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top