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

推荐订阅源

Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
博客园_首页
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
I
InfoQ
量子位
J
Java Code Geeks
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
Webroot Blog
Webroot Blog
Martin Fowler
Martin Fowler
D
Docker
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
罗磊的独立博客
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
Cyberwarzone
Cyberwarzone
P
Privacy & Cybersecurity Law Blog
Last Week in AI
Last Week in AI
爱范儿
爱范儿
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
V
V2EX
Simon Willison's Weblog
Simon Willison's Weblog
AI
AI
Y
Y Combinator Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
V
Visual Studio Blog
H
Heimdal Security Blog
S
Secure Thoughts
B
Blog RSS Feed
雷峰网
雷峰网
T
Tenable Blog
C
Check Point Blog
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - kagar

挖掘经典:几乎被人遗忘的HTML七种用法 【转】P2P穿透NAT原理 关于delphi调用.net com的详细过程 3desj加密 C++ ,C#类型对照 DLL+ ActiveX控件+WEB页面调用例子 测试 表空间 Sql Server2005不同的表使用不同的表空间 Office文档在线编辑的实现之二 如何在Web页面上直接打开、编辑、创建Office文档 Foursquare引爆了什么 承博士:让云计算落地生根的中国云计算平台 PHP - Smarty 工作流系统功能列表系列 五种丑陋的项目管理 Chart 报表 jQuery 数据仓库与事务型数据库的区别 RUP与XP的平衡之道
跨域情况下Iframe高度自适应解决方案 - kagar - 博客园
kagar · 2009-05-08 · via 博客园 - kagar

跨域情况下Iframe高度自适应解决方案

相信很多人的遇到过跨域情况下Iframe高度自适应的问题,在百度和Google上找了很久,都没有找到合适的一解决方案,一次遇然机会,发现了window.navigator这个对象居然在能够在所有的Iframe中均可以访问的对象。下面是基于该对象的一个iframe跨域解决方案
对window.navigator这个对象进行扩展,可以将该js代码存在一个iframe.js文件,以后在父页面调用这个js即可

//声明一个数组用来保存所有的Iframe
window.navigator.Allframes=null;
window.navigator.Allframes=...{''$Top_Page$'':window};
//根据页面name属性查找到子页面所在Ifame对象
window.navigator.getFrameByName=function(oName)...{
    return this.Allframes[oName]
};
//将一个Iframe对象注册到window.navigator.Allframes数组中
window.navigator.registerFrame=function(oName,oElement)...{
    this.Allframes[oName]=oElement
};
//提供一个方法将一个子页面封装成一个对象
window.navigator.createFrame=function(childPage)...{    
    var fun=function()...{    
        this.objChildPage=childPage;
        this.getFrameByName=function(oName)...{
            return window.navigator.getFrameByName(oName)
        };
        this.resizeHeight=function()...{
            try...{
                var height=this.objChildPage.document.body.scrollHeight;
                if(this.objChildPage.name&&height)...{
                    var curIframe=window.navigator.getFrameByName(this.objChildPage.name);
                    curIframe.height=height;
                    return document.body.scrollHeight;
                }
            }catch(ex)...{
                //异常处理
            }
        }
    };
    return new fun
};

父页面parent.html,在http://parent.testdomain.com/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>父页面</title>
    <SCRIPT language="javascript" src="iframe.js" /></script>
    <!--通过CSS的方式为所有的Iframe注册,也可以通过javascript方式调用window.navigator.registerFrame注册指定的Iframe-->
    <STYLE>iframe{behavior:expression(function(element){window.navigator.registerFrame(element.name, element);}(this))}</STYLE>
</head>
<body>
    <iframe id="childPage" name="childPage" src="http://child.testdomain.com/" width="100%" height="50px"  ></iframe>
</body>
</html>
子页面(child.html)在http://child.testdomain.html/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>子页面</title>
    <SCRIPT LANGUAGE="JavaScript">
    //在body.onload中调用
    function AutoResizeHeight(){
      try{
          var control = window.navigator.createFrame(this);
          control.resizeHeight();
       }catch(ex){}
    }
    </script>
</head>
<body onload="AutoResizeHeight()">
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
</body>
</html>