views:

1449

answers:

3

Can I create an empty iframe as a placeholder to later insert html into it?

In otherwords, suppose I have an empty iframe with an id,

How do I insert html in it?

I'm using jquery, if that makes it easier.

+1  A: 

View the source of this page: http://mg.to/test/dynaframe.html It appears to do exactly what you want to do.

$(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 );
});
Tyson
A: 

Yes I know that is possible but the main problem is that we cannot handle a frame from outside it i has already loaded some other thing.

$(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 );
});

but if we start as

<iframe src="http:/www.hamrobrt.com">
<---Your Script to put as you like--->

Then its impossible to hit that out.

Abhilash
+1  A: 

No it's not. You would modify the script like this:

$(function() {
    var $frame = $('iframe');
    setTimeout( function() {
            var doc = $frame[0].contentWindow.document;
            var $body = $('body',doc);
            $body.html('<h1>Test</h1>');
    }, 1 );
});
Adam Campbell