javascript - Iframe document global -


this first js/html problem have came across in months haven't been able find answer online. if dynamically filling iframe using jquery or plain javascript, (so guess same origin parent document), , fill child iframe script in body uses "document" global - ie "$(document).ready", how can ensure accesses child document opposed parent? if fill dynamic iframe following html:

<iframe id='if'><iframe> 

and added following function in script tag:

$(function () {     var innerdoc = $($('#if').contents()[0].childnodes[0]);     innerdoc.html("<scr" + "ipt>" +        "var h1 = document.createelement('h1'); " +        "h1.innertext = 'hi!';" +         "document.body.appendchild(h1);" +     "</scri" + "pt>");  }); 

the html inserted body of parent document. have determined because 'document' variable in script tag being dynamically inserted iframe reference parent document. how ensure exact statement execute correctly, in, 'document' variable reference document of child iframe?

apparently using open() , writeln() make work expected - don't why though.

 var iframedoc = $('#if').contents()[0];  iframedoc.open();  iframedoc.writeln("<html><head></head><body><scr" + "ipt>" +    "var h1 = document.createelement('h1'); " +    "h1.innertext = 'hi!';" +     "document.body.appendchild(h1);" + "</scri" + "pt></body></html>"); iframedoc.close(); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -