您的位置:首页 > 网页制作 > Javascript

JavaScript应用:Iframe自适应其加载的内容高度

日期:2006-09-12 14:35:32 点击: 来自: 作者:

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  language=javascript>
   function iframeAutoFit()
   {
      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 h = document.body.scrollHeight;
                   if(document.all) {h += 4;}
                   if(window.opera) {h += 1;}
                   a[i].style.height = h;
               }
            }
         }
      }
      catch (ex)
      {
         alert("脚本无法跨域操作!");
      }
   }
   if(document.attachEvent)  window.attachEvent("onload",  iframeAutoFit); 
   else  window.addEventListener('load',  iframeAutoFit,  false); 
   </script> 
</head> 
<body> 
   <div  style="width:  200;  height:  400;  background-color:  yellow"> 
       iframe  自适应其加载的网页(多浏览器兼容) 
   </div> 
</body> 
</html>

More..素材图片 Picture Navigation
相关链接 Correlation Link
Javascript热门 Class Hot
Javascript推荐 Class Commend
版权所有:中国网站资源 2005- 未经授权禁止复制或建立镜像 This Site Tech:XHTML+DIV+CSS+Javascript
CopyRight ® 2005- www.chinaddv.com online services. all rights reserved. ICP06016627
Optimized to 1024x768 to Firefox,Netscape,Opera,MS-IE6+.