惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - sinlight23

Web开发框架趋势 ASP.NET MVC - PageData的应用 ASP.NET拾遗 - Health Monitoring .NET Xml序列化时会忽略值为"默认值"的Property或Field ASP.NET MVC 实现模式 - ModelBuilder Enumerable.Range和自定义的IntRange/DateRange HOWTO: Web Deploy时服务器报登录失败的解决方法 HOWTO: 为GitHub for Windows指定代理服务器 ASP.NET MVC - 在MVC 3项目中使用ASP.NET Bundling and Minification机制 脚印:关于错误编码的管理的一些思考 脚印:软件开发随想录 脚印:关于扩展方法的使用 脚印:一次重构讨论 脚印:记录一次重构,将规则生产和规则消费(执行委托)分离 Microsoft ASP.NET 2.0 AJAX 相关信息备忘 MVC 模式在javascript中的应用 VS2010 "SQL Server 2005 Database Project" 使用笔记(二) VS2010 "SQL Server 2005 Database Project" 使用笔记 腳印: 初學者的心態
HOWTO: IE8下处理iframe自适应高度
sinlight23 · 2010-12-24 · via 博客园 - sinlight23

老生常谈的问题, 网络上铺天盖地的处理方法, 还有抄来转去的"博文". 在此主要说明今日遇到的特定于IE8的一个特定问题.

function autoSetFrameHeight(name) {
    try {
        var iframe;
        if (!name) return;
        iframe = document.getElementById(name);
        if (iframe.contentDocument)
            $("#" + name).height(iframe.contentDocument.height + "px");
        else
            $("#" + name).height(document.frames[name].document.body.scrollHeight + "px");
    } catch (e) {
        // alert(e.message);
    }
}

今天有用户反应IE8下某个弹窗(iframe内嵌个小页面)显示不完全, 后经测试, iframe.contentDocument.heightIE8下无效, 在IE6-7, Chrome, Firefox3~4下

改为iframe.contentDocument.body.offsetHeight后, 在IE6-8, Chrome, Firefox3~4下测试通过.

本文的示例代码运行在真实项目中, 但由于iframe目标页面承载的内容较为简单, 因此并未测试iframe目标页面中包含绝对定位层展开/折叠, 以及包含float层的情况,

也未处理某些文章提及的"IE浏览器会有长高了缩不回去"的情形. 请读者注意.

參考資料: (請讀者自行驗證原文內容, 辯證接受)

  1. http://www.cnblogs.com/yuhe7919/archive/2009/11/04/iframe.html
  2. 此文中提及的代碼示例, 建議不要再採用了, 如果第一句取高度的代碼由於瀏覽器兼容性問題產生異常, 則此段代碼完全失效. 另外, 採用兩個高度比較的作法也沒有意義, 有些繞道並且繞遠了的感覺.

    try {

    var bHeight = iframe.contentWindow.document.body.scrollHeight;
    var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;

    } catch(ex) { }