iframe寬高自適應


iframe子頁面結尾添加本script

 

iframe子頁面結尾添加本script

<script type="text/javascript">
        function iframeAuto() {
            try {
                if (window != parent) {
                    //定位需要調整的frame框架(在父級窗口中查找)
                    var a = parent.document.getElementsByTagName("iframe");
                    for (var i = 0; i < a.length; i++) {
                        if (a[i].contentWindow == window) {
                            var h1 = 0, h2 = 0;
                            a[i].parentNode.style.height = a[i].offsetHeight + "px";
                            a[i].style.height = "10px"; //首先設置高度為10px,后面會修改
                            if (document.documentElement && document.documentElement.scrollHeight) {
                                h1 = document.documentElement.scrollHeight;
                            }
                            if (document.body) h2 = document.body.scrollHeight;
                            var h = Math.max(h1, h2); //取兩者中的較大值
                            if (document.all) { h += 4; }
                            if (window.opera) { h += 1; }
                            //調整框架的大小
                            a[i].style.height = a[i].parentNode.style.height = h + "px";
                        }
                    }
                }
            }
            catch (ex) { }
        }
        //事件綁定的方法,支持IE5以上版本
        if (window.attachEvent) {
            window.attachEvent("onload", iframeAuto);
        }
        else if (window.addEventListener) {
            window.addEventListener('load', iframeAuto, false);
        }
   
    </script>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM