You're almost there, you just need to throw in a quick setTimeout before
trying to write to the frame document. I ran into this some time ago when I
was doing the same thing.

Also, you can simplify the contentWindow thing a bit. Try this code:

    $(function() {
        var $frame = $('<iframe style="width:200px; height:100px;">');
        $('body').html( $frame );
        setTimeout( function() {
            var doc = $frame[0].contentWindow.document;
            var $body = $('body',doc);
            $body.html('<h1>Test</h1>');
        }, 1 );
    });

Test page:

http://mg.to/test/dynaframe.html 

-Mike

> From: Geuis
> 
> Hi folks, hoping for some knowledgeable assistance with this problem.
> 
> What I'm doing is dynamically creating an iframe in the DOM, then
> trying to write some data into it. That's the part I'm stuck at.
> 
> I *am* able to create the iframe, no problem. I have a div with the id
> #container built into the page in which the iframe is added:
> 
> $
> (document.createElement('iframe')).attr('id','commentiframe').
> appendTo('#container');
> 
> At this point, I don't know how to write data into the iframe that has
> been created. I've been trying to use this but to no success.
> 
> var f = $('#commentiframe')[0];
> // get iframe's document
> var doc = f.contentWindow ? f.contentWindow.document :
> f.contentDocument ? f.contentDocument : f.document;
> var $framebody = $('body',doc);
> var $framebody.html('<h2>test</h2>');
> 

Reply via email to