Open Bug 695868 Opened 8 years ago Updated 3 months ago

Unable to set document.body.innerHTML of IFrame

Categories

(Core :: DOM: Core & HTML, defect, P3)

defect

Tracking

()

Webcompat Priority revisit

People

(Reporter: theosib, Unassigned)

References

(Depends on 1 open bug)

Details

(Whiteboard: [webcompat][needs-wpt-?])

Attachments

(2 files)

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.
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.
Attached file the testcase
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.
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: sync-about-blank
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.
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]
Flags: webcompat?
Priority: -- → P3

Migrating Webcompat whiteboard priorities to project flags. See bug 1547409.

Webcompat Priority: --- → ?

See bug 1547409. Migrating whiteboard priority tags to program flags.

(In reply to Henri Sivonen (:hsivonen) from comment #7)

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

Henri, is it the HTML standard that (should?) defines this?

Flags: needinfo?(hsivonen)
Whiteboard: [webcompat] → [webcompat][needs-wpt-?]

(In reply to Mike Taylor [:miketaylr] (PTO July 31 - Aug 8) from comment #13)

Henri, is it the HTML standard that (should?) defines this?

This is in the HTML Standard, but a quick look suggests the level of detail is insufficient (about events):
https://html.spec.whatwg.org/#creating-a-new-browsing-context

Flags: needinfo?(hsivonen)
Webcompat Priority: ? → revisit
You need to log in before you can comment on or make changes to this bug.