2009-03-06 16 views

risposta

30

lo si può fare senza jQuery anche:

var iframe = document.getElementById('iframeID'); 
iframe = iframe.contentWindow || (iframe.contentDocument.document || iframe.contentDocument); 

iframe.document.open(); 
iframe.document.write('Hello World!'); 
iframe.document.close(); 

di jQuery strisce html corpo, html e la testa tag dal HTML inserito.

+0

Questa è la risposta corretta. ''. Si noti che l'iframe deve essere inserito nel suo documento genitore. –

+0

Funziona come un fascino quando si caricano script "document.write" da richieste AJAX –

+2

'iframe = iframe.contentWindow || (iframe.contentDocument.document || iframe.contentDocument); ' – Nildarar

36

Visualizza la sorgente di questa pagina: http://mg.to/test/dynaframe.html Sembra fare esattamente quello che vuoi fare.

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

Questa copia solo il 'body' elemento, quando potrebbe essere necessario l'elemento' head', o anche gli attributi dell'elemento 'html'. – Flimm

1

sì, lo so che è possibile, ma il problema principale è che non siamo in grado di gestire una struttura da fuori di essa i è già caricato qualche altra cosa.

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

ma se partiamo come

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

Poi la sua impossibilità di colpire che fuori.

8

No, non lo è. Si potrebbe modificare lo script come questo:

$(function() { 
    var $frame = $('iframe'); 
    setTimeout(function() { 
      var doc = $frame[0].contentWindow.document; 
      var $body = $('body',doc); 
      $body.html('<h1>Test</h1>'); 
    }, 1); 
}); 
2
iframeElementContainer = document.getElementById('BOX_IFRAME').contentDocument; 
iframeElementContainer.open(); 
iframeElementContainer.writeln("<html><body>hello</body></html>"); 
iframeElementContainer.close(); 
0

ho lo stesso problema, Dove devo mostrare snippet di codice HTML in iframe dinamicamente da database senza l'attributo SRC di IFRAME e ho fissato lo stesso problema con il seguente codice

Spero che questo avrebbe aiutato qualcuno chi ha lo stesso requisito che ho avuto.

jsfiddle example here

HTML:

<iframe src="" frameborder="0" class="iframe"></iframe> 
<iframe src="" frameborder="0" class="iframe"></iframe> 
<iframe src="" frameborder="0" class="iframe"></iframe> 
<iframe src="" frameborder="0" class="iframe"></iframe> 

JS

<script> 
var doc,$body; 
var txt = Array(
      '<style>body{background-color:grey} h1{background-color:red;font-weight:900}</style><h1>Cool!!! this is text for</h1><p>First Iframe</p>', 
      '<style>body{background-color:pink}h1{background-color:green;font-weight:200}</style><h1>Cool This is Text for</h1><p> second Iframe', 
      '<style>body{background-color:yellow}h1{font-weight:400}</style><h1>Cool..... This is text FOR : </h1> <div>3rd Iframe</div>', 
      '<style>body{background-color:blue} h1{font-weight:400}</style><h1>Cool This is text for Fourth iframe</h1>' 
      ); 
$('.iframe').each(function(index,value){ 
    doc = $('iframe')[index].contentWindow.document; 
     $body = $('body',doc); 
     $body.html(txt[index]); 
}); 
</script> 
Problemi correlati