سؤال

i use such construction in iframe to send its height to parent window:

<body onload="parent.postMessage(document.body.scrollHeight, '*');">
    ...
</body>

there is a listener in parent window:

function resizeCrossDomainIframe(id)
{
    var iframe = document.getElementById(id);
    window.addEventListener('message', function (event)
    {
        if (isNaN(event.data)) return;
        var height = parseInt(event.data);
        iframe.height = height + "px";
    }, false);
}

<iframe id="voice-iframe" scrolling="no" frameborder="0" onload="resizeCrossDomainIframe('voice-iframe');" src="http://localhost:2040/VoiceApi/Base">
</iframe>

this code works in ff, chrome, safari, ie9, probably in ie8, but not in opera 11 (though no errors are shown in dragonfly). i thought there will be some problems with ie but not with the last version of opera. or maybe i'm doing smth wrong?

هل كانت مفيدة؟

المحلول

I think onload fires inside the IFRAME first, postMessage() will then cause a message event to happen, and then finally the onload attribute on the IFRAME will trigger. If you add the event listener from the IFRAME tag's onload handler, you'll probably start listen for the message after it was sent, so you won't receive anything.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top