Manipular dados Iframe recem criado
Olá,
Estou apenas brincando com o javascript e nessa brincadeira me surgiu uma dúvida.
Vamos lá:
Estou criando um Iframe dinamicamente e querendo alterar o conteúdo do mesmo.
<script>
function printDiv(id) {
var div = document.getElementById(id);
var content = div.innerHTML;
var iframe = document.createElement('IFRAME');
iframe.src = 'printable.html';
iframe.height = 190;
iframe.width = 190;
iframe.id = 'iframePrintable';
iframe.name = 'iframePrintable';
//iframe.style = 'display: none;';
document.body.insertBefore(iframe, document.getElementById('printable'));
}
</script>
A questão é: Como manipular os dados desse iframe?
Tentei:
<script>
function printDiv(id) {
var div = document.getElementById(id);
var content = div.innerHTML;
var iframe = document.createElement('IFRAME');
iframe.src = 'printable.html';
iframe.height = 190;
iframe.width = 190;
iframe.id = 'iframePrintable';
iframe.name = 'iframePrintable';
//iframe.style = 'display: none;';
document.body.insertBefore(iframe, document.getElementById('printable'));
}
function changeContent() {
var frame = window.frames['iframePrintable'];
console.log('Frame:');
console.log(frame.document);
console.log('Fim Frame');
}
</script>
Ele retorna o documento em branco:
>
Frame: t.php:36
#document<html><head></head><body></body></html> t.php:37
Fim Frame
Estou chamando as funções da seguinte forma:
<button onclick="printDiv('printable');changeContent()" id="toClick">Print</button>
Já tentei também fazer tudo em uma função, se for null a div que eu quero pegar do iframe, dar um novo click pra executar novamente a função.
Alguma idéia?
Discussão (1)
Carregando comentários...