首页  :: 新随笔  :: 联系 :: 管理

IFrame自适应高度

Posted on 2010-02-17 16:45  ^_^克方  阅读(215)  评论(0)    收藏  举报

  原文可以访问下面连接:http://blog.csdn.net/meizz/archive/2006/03/27/639857.aspx

代码
<script type="text/javascript" language="javascript">

        
function iframeAutoFit() {
            
try {
                
if (window != parent) {                   
                    
var frame = parent.document.getElementById("contentIframe");
                    
if (frame.contentWindow == window) {
                        
var h1 = 0, h2 = 0, d = document, dd = d.documentElement;
                        frame.parentNode.style.height 
= frame.offsetHeight + "px";
                        frame.style.height 
= "10px";

                        
if (dd && dd.scrollHeight) h1 = dd.scrollHeight;
                        
if (d.body) h2 = d.body.scrollHeight;
                        
var h = Math.max(h1, h2);

                        
if (document.all) { h += 4; }
                        
if (window.opera) { h += 1; }
                        frame.style.height 
= frame.parentNode.style.height = h + "px";
                    }

                }
            }
            
catch (ex) { }
        }
        
if (window.attachEvent) {
            window.attachEvent(
"onload", iframeAutoFit);
            
//window.attachEvent("onresize",  iframeAutoFit);
        }
        
else if (window.addEventListener) {
            window.addEventListener(
'load', iframeAutoFit, false);
            
//window.addEventListener('resize',  iframeAutoFit,  false);
        }
    
</script>