Unable to set document.body.innerHTML of IFrame

NEW
Unassigned

Status

()

Core
DOM: Core & HTML
7 years ago
22 days ago

People

(Reporter: Timothy Miller, Unassigned)

Tracking

(Depends on: 1 bug)

Trunk
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [webcompat])

Attachments

(2 attachments)

(Reporter)

Description

7 years ago
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22

Steps to reproduce:

I'm using the nightly, and 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/).

This is what I wrote:


<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>


Actual results:

It works fine in Safari and Chrome, but not in the latest nightly of Firefox.


Expected results:

I should see the word "NETSCAPE" appear in the blue box.
(Reporter)

Comment 1

7 years ago
Someone suggested I access the innerHTML this way:

frames['IFRAME2'].document.documentElement.innerHTML = '<html><body><p>NETSCAPE</p></body></html>';

But that doesn't work either.

Comment 2

7 years ago
Created attachment 568340 [details]
the testcase

Comment 3

7 years ago
Confirming on Win XP and FF10a1.
Interestingly, after loading the page, running the command:

document.getElementById("IFRAME2").contentWindow.document.body.innerHTML = '<html><body><p>NETSCAPE</p></body></html>';

from the Web Console does work.
Status: UNCONFIRMED → NEW
Component: General → DOM: Core & HTML
Ever confirmed: true
Product: Firefox → Core
QA Contact: general → general
OS: Mac OS X → All
Hardware: x86 → All
I think the reason is because the document inside your iframe isn't created yet.
You should wait for the load event before setting something inside the iframe.

I'm going to attach an example that is working.
Created attachment 568350 [details]
working example
Henri, do we plan to follow what Webkit is doing here or should we WONTFIX this? (or DUPE maybe?)
(In reply to Mounir Lamouri (:volkmar) (:mounir) from comment #6)
> Henri, do we plan to follow what Webkit is doing here or should we WONTFIX
> this?

I expect us to follow WebKit/spec here eventually. Marking as dependent on bug 543435.
Depends on: 543435
(Reporter)

Comment 8

7 years ago
Someone suggested a workaround for this.  Before setting the innerHTML, we can trigger a false load.  Here's the new version that works, without needing to wait for an unload, which would be inconvenient for the real application (my code here is just the essential parts that demonstrate the problem):

<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>';
doc = frames["IFRAME2"].document;
doc.open();
doc.close();
doc.body.innerHTML = '<html><body><p>NETSCAPE</p></body></html>';
</script>

</body>
</html>

I got the suggestion from here:
http://stackoverflow.com/questions/7828502/cannot-set-document-body-innerhtml-of-iframe-in-firefox
any news for this?

As you can see http://output.jsbin.com/yipemaweha this in Firefox not work, in chrome there are no problem.

Comment 10

3 months ago
This long-standing interop issue is still affecting sites adversely, so it's probably worth reconsidering its priority again after all this time.
Flags: webcompat?
Whiteboard: [webcompat]

Updated

2 months ago
Flags: webcompat?
You need to log in before you can comment on or make changes to this bug.