转自:http://cll.socss.cn/archives/589
frame高度自动适应相信已经被谈论过无数次了,网上有很多种解决的办法。下面的代码经过应经上线的网站测试可以解决iframe高度自动适应的问题。
兼容IE6,7,8,FF,chrome。
01 | <script type= "text/javascript" > |
02 | function SetCwinHeight(obj){ |
04 | if (document.getElementById){ |
05 | if (cwin && !window.opera) |
07 | if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight) |
08 | {cwin.height = cwin.contentDocument.body.offsetHeight;} |
09 | else if (cwin.Document && cwin.Document.body.scrollHeight) |
10 | cwin.height = cwin.Document.body.scrollHeight; |
在一次需要用到iframe高度自动适应的项目中用上面的代码测试了一下,chrome下无效。折腾了很久,找了很多可能的原因,都不是。
更郁闷的是用同样代码的应经正式运营的网站在chrome下是正常的。
后来发现显示正常的网站都是绝对路径,于是就把本地文件上传到服务器,用绝对路径打开,发现用chrome打开显示正常。