不能在Firefox中设置IFrame的document.body.innerHTML [英] Cannot set document.body.innerHTML of IFrame in Firefox
问题描述
< html>
< body>
< div name = FRAME2div id = FRAME2div style =position:absolute; background-color:blue; color:black; border-color:black; border-width:0; left:100px; top:40px; width:200px; height:200px; overflow:scroll; display:block;>
< / div>
< script type =text / javascript>
document.getElementById(FRAME2div)。innerHTML ='< iframe border = 0 id = IFRAME2 name = IFRAME2>< / iframe>';
document.getElementById(IFRAME2)。contentWindow.document.body.innerHTML ='< html>< body>< p> NETSCAPE< / p>< / body>< / html>' ;
< / script>
< / body>
< / html>
在Firefox中,框架的内容似乎不被识别当没有初始内容被设置。下面的代码显示了解决这个问题的最简单方法:
//注意:在这个例子中ID不是必须的
document.getElementById(FRAME2div)。innerHTML ='< iframe id = IFRAME2 name = IFRAME2>< / iframe>';
var doc = frames [IFRAME2]。
//触发一个页面加载。
doc.open();
doc.close();
//设置body标签的innerHTML。
doc.body.innerHTML ='< p> NETSCAPE< / p>';
另一种方法是设置 src
属性到 javascript:& nbsp;
,并注册一次性载入事件处理程序。这个方法稍微复杂一点,所以我没有详细描述它。
每一帧的窗口对象都可以通过
框架
对象,按名称访问。所以,我推荐使用下面的代码: frames ['IFRAME2']。document.documentElement.innerHTML =< body> ...< / body>;
document.documentElement
指向根元素通常是< html>
。当您调用当前的代码时,文档
的主体
属性可能尚未准备好。通过引用根元素,这个问题就被绕过了。
I've looked for a cross-browser way to programmatically set the innerHTML of an IFrame. (As in http://roneiv.wordpress.com/2008/01/18/get-the-content-of-an-iframe-in-javascript-crossbrowser-solution-for-both-ie-and-firefox/). I've written some sample code (below), and I can get it to work in Safari and Chrome, but not Firefox. Can anyone help me figure out what I need to do instead (in a portable way)?
<html>
<body>
<div name=FRAME2div id=FRAME2div style="position:absolute; background-color:blue; color:black; border-color:black;border-width:0; left:100px; top:40px; width:200px; height:200px; overflow:scroll; display:block; " >
</div>
<script type="text/javascript">
document.getElementById("FRAME2div").innerHTML = '<iframe border=0 id=IFRAME2 name=IFRAME2 ></iframe>';
document.getElementById("IFRAME2").contentWindow.document.body.innerHTML = '<html><body><p>NETSCAPE</p></body></html>';
</script>
</body>
</html>
In Firefox, the frame's content seems to not be recognised when no initial content has been set. The easiest method to solve this, is shown in the code below:
//Note: ID is not necessary for this example
document.getElementById("FRAME2div").innerHTML = '<iframe id=IFRAME2 name=IFRAME2 ></iframe>';
var doc = frames["IFRAME2"].document;
//Trigger a page "load".
doc.open();
doc.close();
//Set innerHTML of the body tag.
doc.body.innerHTML = '<p>NETSCAPE</p>';
Another method consists of setting the src
property to javascript:" "
, and register an one-time load event handler. This method is slightly more complex, hence I do not describe it in a deeper detail.
Every frame's window object can be accessed through the
frame
object, by name.
So, I recommend to use the following code:
frames['IFRAME2'].document.documentElement.innerHTML = "<body>...</body>";
document.documentElement
refers to the root element of a document, usually <html>
. It's possible that the body
property of document
isn't ready yet, when you call your current code. By referring to the root element, this problem is circumvented.
这篇关于不能在Firefox中设置IFrame的document.body.innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!