我在iframe中使用这样的结构将其高度发送到父窗口:
<body onload="parent.postMessage(document.body.scrollHeight, '*');">
...
</body>父窗口中有一个监听程序:
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>这段代码可以在ff,chrome,safari,ie9中运行,可能在ie8中运行,但不能在Opera11中运行(虽然在dragonfly中没有显示错误)。我认为ie会有一些问题,但不是最后一个版本的歌剧。或者也许我做错了什么?
发布于 2011-09-13 06:42:01
我认为onload首先在IFRAME内触发,然后postMessage()将导致一个消息事件发生,最后IFRAME上的onload属性将被触发。如果从IFRAME标记的onload处理程序添加事件侦听器,则可能会在消息发送后开始侦听消息,因此不会收到任何消息。
https://stackoverflow.com/questions/7348813
复制相似问题