Access to inner iframe

by wao20

JavaScript

// I borrowed the injection script from here: 
// http://sparecycles.wordpress.com/2012/03/08/inject-content-into-a-new-iframe/
var newIframe = document.createElement('iframe');
newIframe.width = '400';newIframe.height = '200';
newIframe.src = 'about:blank'; 
newIframe.id = 'f123';
document.body.appendChild(newIframe);

var myContent = '<!DOCTYPE html>'
    + '<html><head><title>My dynamic document</title></head>'
    + '<body><h1>Hello world</h1><input id="secret" type="text" value="Luke, I am your father" /></body></html>';

newIframe.contentWindow.document.open('text/html', 'replace');
newIframe.contentWindow.document.write(myContent);
newIframe.contentWindow.document.close();


// This script run when the page is loaded
var f = document.getElementById('f123');
if (f != undefined){
    var doc = f.contentDocument || f.contentWindow.document;
    var txt = doc.getElementById('secret');
    alert(txt.value);
}
else {
    alert('No iframe, no secret!');
}