JavaScript應用:Iframe自適應其加載的內容高度

標題       JavaScript應用:Iframe自適應其加載的內容高度     選擇自 meizz 的 Blog 
關鍵字   JavaScript應用:Iframe自適應其加載的內容高度

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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章