Iframe自动适应加载内容高度

最近在做项目的时候,我发现Iframe可以自动适应加载内容高度,觉得挺新奇,想研究一下这段JS。原来是用到meizz(梅花雪)写的JS,真佩服meizz,可以说写JS已经出神入化了。真想认识一下meizz,看能不能通过朋友介绍认识一下,那真是万分荣幸了。好了,该show一下了。

main.htm:

<html>  
    
<head>  
       
<meta http-equiv='Content-Type'  content='text/html; charset=gb2312' />  
       
<meta  name='author' content='F.R.Huang(meizz梅花雪)//www.meizz.com' />  
       <title>iframe自适应加载的页面高度</title>  
    
</head>  
     
    
<body>
        
<iframe src="child.htm"></iframe>
    
</body>
</html>

child.htm:

<html>  
<head>  
    
<meta  http-equiv='Content-Type'  content='text/html;  charset=gb2312' />  
    
<meta  name='author'  content='F.R.Huang(meizz梅花雪)//www.meizz.com' />  
    <title>iframe  自适应其加载的网页(多浏览器兼容)</title>  
    
<script type="text/javascript">
    
<!--
    
function iframeAutoFit()
    
{
        
var ex;
        
try
        
{
            
if(window!=parent)
            
{
                
var a = parent.document.getElementsByTagName("IFRAME");
                
for(var i=0; i<a.length; i++//author:meizz
                {
                    
if(a[i].contentWindow==window)
                    
{
                        
var h1=0, h2=0;
                        
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 
= h +"px";
                    }

                }

            }

        }

        
catch (ex){}
    }

    
if(document.attachEvent)
    
{
        window.attachEvent(
"onload",  iframeAutoFit);
        window.attachEvent(
"onresize",  iframeAutoFit);
    }

    
else
    
{
        window.addEventListener('load',  iframeAutoFit,  
false);
        window.addEventListener('resize',  iframeAutoFit,  
false);
    }

    
//-->
    </script>  
</head>  
<body>
    
<table border="1" width="200" style="height: 400px; background-color: yellow">
        
<tr>
            
<td>iframe  自适应其加载的网页(多浏览器兼容,支持XHTML)</td>
        
</tr>
    
</table>
</body>  
</html>
posted on 2007-08-14 16:15  Yolion  阅读(562)  评论(0编辑  收藏  举报