The problem was solved using "postMessage", remembering that the problem arose because I tried to access the contents of an iframe in which "src" did not point to the same domain.
Basically what the code below does is to send a message to iframe after being loaded (onload) using "postMessage".
Inside the iframe is an event that awaits the message. When the message is received iframe responds (also through "postMessage") with the height of its contents.
In the parent element where iframe was inserted, there is another event waiting for the message with the content height.
Script of the page where the iframe was inserted:
//Gerando o iframe
var iframe = document.createElement('iframe');
as_root.appendChild(iframe);
iframe.id = "as-root-iframe";
iframe.style.width = '100%';
iframe.style.border = 'none';
iframe.style.scrolling = 'no';
iframe.style.overflow = 'hidden';
iframe.onload = function() {
cpframe = document.getElementById('as-root-iframe').contentWindow;
cpframe.postMessage('iframe-ok','*');
}
iframe.src = "http://dominio.como/embed/DYaok87sd";
//evento que aguarda a mensagem com a altura do conteúdo e posteriormente altera a altura do iframe
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
eventer(messageEvent,function(e) {
document.getElementById('as-root-iframe').style.height = e.data+'px';
},false);
Script inside the iframe:
//evento que aguarda mensagem do elemento "pai" solicitando a altura do conteúdo
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
eventer(messageEvent,function(e) {
var h = document.getElementById('as_content').offsetHeight+50;
window.parent.postMessage(h,"*");
},false);
It was necessary to do everything with pure Javascript because it was not possible to use frameworks like Jquery.
Take a look here: http://stackoverflow.com/questions/5908676/yet-another-cross-domain-iframe-resize-qa
– André Ribeiro