gpt4 book ai didi

javascript - 为什么 postMessage 脚本在 IE8 中不起作用?

转载 作者:数据小太阳 更新时间:2023-10-29 03:53:41 24 4
gpt4 key购买 nike

经过广泛的研究,这似乎应该可行,但在 IE8 中,letsgo 函数永远不会被调用...有什么帮助吗?

<script type="text/javascript">
function resizeCrossDomainIframe() {

if (window.addEventListener) {
window.addEventListener('message', letsgo, false);
} else if (window.attachEvent) {
window.attachEvent('onmessage', letsgo);
}
}
function letsgo(event) {
var iframe = document.getElementById('my_iframe');
if (event.origin !== 'http://mysite.com') return; // only accept messages from the specified domain
if (isNaN(event.data)) return; // only accept something which can be parsed as a number
var height = parseInt(event.data) + 32; // add some extra height to avoid scrollbar
iframe.height = height + "px";
}
</script>
<iframe src='http://mysite.com/products/default.aspx?iframe=true&partnerid=222&site=localhost:62014' frameborder="0" width="100%" scrolling="auto" style="min-height: 750px; min-width: 600px; background-color: #fff;" id="my_iframe" onload="resizeCrossDomainIframe();">
</iframe>

最佳答案

我明白了,一定是竞争条件。我把onload拿出来了。

<script type="text/javascript">


if (window.addEventListener) {
window.addEventListener('message', letsgo, false);
} else if (window.attachEvent) {
window.attachEvent('onmessage', letsgo);
}

function letsgo(event) {
var iframe = document.getElementById('my_iframe');
if (event.origin !== 'http://mysite.com') return; // only accept messages from the specified domain
if (isNaN(event.data)) return; // only accept something which can be parsed as a number
var height = parseInt(event.data) + 32; // add some extra height to avoid scrollbar
iframe.height = height + "px";
}
</script>
<iframe src='http://mysite.com/products/default.aspx?iframe=true&partnerid=222&site=localhost:62014' frameborder="0" width="100%" scrolling="auto" style="min-height: 750px; min-width: 600px; background-color: #fff;" id="my_iframe" >
</iframe>

关于javascript - 为什么 postMessage 脚本在 IE8 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14713339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com