Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

javascript - Firefox not able to find iframe

This is the iframe I'm trying to access:

 <div class="mceBody" id="additionalTxt_b">
        <iframe frameborder="0" id="additionalTxt_f" src='javascript:""' class="punymce"/>
 </div>

Using this line:

frames['additionalTxt_f'].document.getElementsByTagName("body")[0].innerHTML

For some reason I'm getting "frames.additionalTxt_f is undefined" from firebug. I have similar iframes (dynamically created by punyMCE plugin) on other pages, and they work perfectly fine. And IE7/8 has no problem accessing this iframe either.

Just at a complete loss here. Any ideas on why Firefox can't find the iframe?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The window.frames[] array is indexed by the [i]frame's name attribute (aka frame target). id can't be relied upon to also work?—?although it may in IE <8, which often thinks names and ids are the same thing.

If you want to access a frame's content via ID, use the DOM Level 2 HTML contentDocument property instead of the old-school (“DOM Level 0”) frames array:

document.getElementById('additionalTxt_f').contentDocument.body.innerHTML

...but then, for compatibility with IE <8, you also have to add some fallback cruft, since it doesn't support contentDocument:

var f= document.getElementById('additionalTxt_f');
var d= f.contentDocument? f.contentDocument : f.contentWindow.document;
d.body.innerHTML

So it's up to you which method you think is less ugly: the extra script work, or just using the name attribute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...