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

时间:2024-02-25 13:46:07 

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>

标签:javascript应用,Iframe自适应其加载的内容高度
0
投稿

猜你喜欢

  • Python调用Windows API函数编写录音机和音乐播放器功能

    2021-06-13 19:03:55
  • js 轮播效果实例分享

    2023-07-16 00:42:43
  • Python 3.8 新功能大揭秘【新手必学】

    2021-07-23 18:08:50
  • Python实现解析命令行参数的常见方法总结

    2023-10-15 12:31:49
  • Python实现约瑟夫环问题的方法

    2021-09-07 19:41:28
  • ASP.NET Core中的Configuration配置二

    2024-06-05 09:33:11
  • opencv python模糊影像检测效果

    2021-07-27 04:44:13
  • python+selenium对table表和分页处理

    2024-01-04 02:16:18
  • Python3操作SQL Server数据库(实例讲解)

    2024-01-24 04:13:21
  • PHP简易延时队列的实现流程详解

    2023-05-29 23:02:48
  • ASP所有的Session变量获取实现代码

    2011-03-11 10:44:00
  • python实现通过队列完成进程间的多任务功能示例

    2022-06-25 03:49:27
  • 使用python计算方差方式——pandas.series.std()

    2022-05-24 14:03:35
  • ACCESS中Field对象的标题属性

    2008-11-20 17:44:00
  • pytorch学习教程之自定义数据集

    2021-06-09 03:13:16
  • Python Pandas如何获取和修改任意位置的值(at,iat,loc,iloc)

    2024-01-01 23:16:43
  • 网页设计者应当注意九大要点

    2007-08-10 13:30:00
  • ASP模拟MVC模型的编程方式

    2008-10-15 14:51:00
  • Python干货实战之八音符酱小游戏全过程详解

    2021-08-20 11:21:27
  • Vue3响应式对象Reactive和Ref的用法解读

    2024-05-02 16:34:48
  • asp之家 网络编程 m.aspxhome.com