Open Bug 728151 Opened 12 years ago Updated 2 years ago

Dynamically created iframe contents don't stay if script that creates iframe is dynamically created without src

Categories

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

9 Branch
x86_64
Linux
defect

Tracking

()

UNCONFIRMED
Webcompat Priority P3

People

(Reporter: kishore.ssn, Unassigned)

References

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.11 Safari/535.19

Steps to reproduce:

I create an IFrame using the following code.

 <html>  
    <head>  
        <script>  
        function Start() {  
            var iframe = document.createElement("iframe");
            document.body.appendChild(iframe);
            iframe.contentWindow.document.write("DynamicIframe");
	}

	function create() {
	  var script = document.createElement('script');
	  script.text = 'Start();';
	  document.body.appendChild(script);
	}
        </script>  
    </head>  
    <body>  
	<script>setTimeout(create, 10);</script>
    </body>  
</html>    


Actual results:

Blank iframe is displayed on screen. I see the text 'DynamicIframe' inside iframe for a moment and disappears.

This Test works well with all other browsers like chrome, safari, IE


Expected results:

IFrame should have 'DynamicIframe' text inside.
Summary: Updating IFrame contents through Javascript doesn't stay → Dynamically created iframe contents don't stay if script that creates iframe is dynamically created without src
Attached file Nasty workaround
For some unknown reason this only works if inside the create() function the js content is added in the src attribute instead of inside the tags.

Changing setTimeout(create, 10); to setTimeout(Start, 10); also works but I'm guessing bypassing the create() function entirely isn't desired.

Hope this helps pinpoint the bug.
Yes., Calling "Start" works well. But inserting a script node with Start in it has this problem. 
I see that iframe that is created in Start gets replaced with an empty body after a while. This can be see by setting an attribute to the body of the iframe inside Start.

   iframe.contentWindow.document.body.setAttribute('a', '10');

This attribute 'a' gets set inside 'Start', but when text in iframe disappears, the attribute also gets removed.
Confirm User Agent:
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0"
"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0"

Also here is my example of how I came to this bug http://pastebin.com/CzzfB709
Mostly the same steps, but I've dynamically created all JS that creates iframe.
Component: Untriaged → DOM
Product: Firefox → Core
In Firefox, you need to do it like this:

    iframe.onload = function() {
        iframe.contentDocument.body.innerHTML = "DynamicIframe";
    };

The important thing - whatever you want to do - is to do it within/after onload(). This method also works in other browsers, so you don't need special/separate code to make it work in Firefox too.

This is an old issue. Other browsers will let you write to the iframe immediately, but not Firefox. I don't know why they chose to make it necessary like this, but it's not been changed in many years. I guess they have their reasons for it, and have no plan to change it as far as I can tell.
Priority: -- → P5
Component: DOM → DOM: Core & HTML

(we've seen a WebCompat report with a lab-situation here. Even though this is likely a webcompat-p3, I'm still nominating it so this is easier to discover.)

Webcompat Priority: --- → ?
Webcompat Priority: ? → P3
Severity: normal → S3

Not sure if my use-case fits here, but I have a jQuery app that takes a Markdown code-block, looks for filenames listed above it (e.g. mycss.css, myjs.js) and then injects an iframe into the page with the code from from the Markdown code-block and the JS and CSS added to the HEAD of the iframe. It's for documenting code (a design system).

Works in Chrome, in Firefox the empty iframe displays the rendered HTML, with JS and CSS applied, as it should, but for only 1/10th of a second, then blank. If I refresh repeatedly I see the briefest flashes of the code, but it is always blank at rest.

I am not an exemplary JavaScript coder, but as far as I can see I am not making any huge faux pas, console is not reporting errors, Chrome is 100% solid.

Is it me (I need to change my code perhaps) or is this a temporary issue with Ffox or is Ffox being permanently more-secure(?) and not allowing this type of process for some reason?

Thanks for any coment. -Alan

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: