2012-08-28 18 views
6

Aşağıdaki kodda iframe'in contentDocument ve contentWindow içeriğine erişmeye çalışıyorum. Ama ikisi de boş.iframe contentDocument ve contentWindow boş

var iframe = document.createElement('iframe'); 
    this.get__domElement$i().appendChild(iframe); 
    if (iframe.contentDocument) { 
     iframe.contentDocument.write("Hello"); 
    } 
    else if (iframe.contentWindow) { 
     iframe.contentWindow.document.body.innerHTML = "Hello"; 
    } 

Birisi bana bunun nesi yanlış olduğunu söyleyebilir mi? Teşekkürler

Document.Body.AppendChild (iframe) yaptığımda, contentDocument ve contentWindow boş değil.

iframe'i div'e eklediğimde birileri neyin yanlış olduğunu söyleyebilir mi?

Çok teşekkürler. değildi div i iframe eklemek çalışıyordu çünkü

Seninle aynı hata var:

cevap

2

Ben bunu biraz geç olduğunu biliyorum, ama bu araştırma ve sorunuza tökezledi iframe eklemeye çalışırken yüklü. div yüklenirse kodunuzu çalışır:

İşte
<!DOCTYPE html> 
<html> 
    <head> 
    </head> 
    <body> 
     <div id='test' > 

     </div> 
     <script> 
      var iframe = document.createElement('iframe'); 
      var myDiv = document.getElementById('test'); 
      myDiv.appendChild(iframe); 
      if (iframe.contentDocument) { 
       iframe.contentDocument.write("Hello"); 
      } 
      else if (iframe.contentWindow) { 
       iframe.contentWindow.document.body.innerHTML = "Hello"; 
      } 
     </script> 
    </body> 
</html> 

bir hata TypeError: Cannot call method 'appendChild' of null vermekte etiketin bulunduğu bir bu çalışma kodu ile jsFiddle ve another olduğunu.