如何为所有浏览器设置iFrame内容的高度?

时间:2011-10-19 23:37:27

标签: javascript jquery asp.net iframe

如果iframe的内容是带有母版页的asp.net网站,如何设置iFrame内容的高度?

有没有人知道任何jQuery插件?

更新 的 这在IE中工作正常但在chrome和firefox中失败

    function resizeFrame(f) {
        f.style.height = f.contentWindow.document.body.scrollHeight + "px";
    }

在body onload()上调用它

  

body onload =“resizeFrame(document.getElementById('Mainiframe'))”

iframe代码

        <iframe id="MainIFrame" class="autoHeight" runat="server" marginwidth="0"    style="margin: auto; " 
                frameborder="0" width="100%" scrolling="no" >
                </iframe>

1 个答案:

答案 0 :(得分:2)

脚本适用于所有浏览器

<script type="text/javascript" language="javascript">
        function resizeIframe(dynheight) {
            try {
                var f = document.getElementById("autosizeframe");
                document.getElementById("autosizeframe").height = parseInt(dynheight) + 10;
                if (f.contentDocument) {
                    f.height = f.contentDocument.documentElement.scrollHeight + 30; //FF 3.0.11, Opera 9.63, and Chrome
                } else {
                    f.height = f.contentWindow.document.body.scrollHeight + 30; //IE6, IE7 and Chrome

                }
            }

            catch (err) {
                alert('Err: ' + err.message);
                window.status = err.message;
            }
        }

        window.onload = function() {
            var height = document.body.scrollHeight;
            resizeIframe(height);

        };                                

    </script>
相关问题