10

经过广泛的研究,这似乎应该可行,但在 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>
4

1 回答 1

7

我明白了,一定是比赛条件。我拿出了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>
于 2013-02-05T19:36:58.633 回答