代码改变世界

iframe自适应高度(简单经典)兼容ie6-ie9 ,firefox,opera,chrome

2013-05-30 23:12  robinli  阅读(894)  评论(0编辑  收藏  举报

一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 解决了以前头痛的浏览器兼容性问题,最近客户发现项目在谷歌浏览器Chrome中死活不兼容,折腾了大半天,终于找到完美解决方法,发出来共享,希望对同行有帮助!

 

代码非常简单在需要自适应高度的iframe里面调用iFrameHeight函数即可

经过ie6-ie9 ,firefox,opera,chrome测试完全兼容

你可以自己创建一个iframe页面来测试

ps:记得给iframe加id和name值

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>iframe自适应高度</title> <script type="text/javascript" language="javascript">     function iFrameHeight(id,name) {    var ifm= document.getElementById(id);      var subWeb = document.frames ? document.frames[name].document : ifm.contentDocument;      if(ifm != null && subWeb != null) {      ifm.height = subWeb.body.scrollHeight;   }     }    </script> </head> <body> <iframe src="zi.html" id="iframepage" name="iframepage" frameBorder=0 scrolling=no width="100%" onLoad="iFrameHeight(this.id,this.name)" ></iframe> </body> </html>