经常有项目会要求实现iframe高度自适应,如果是同域的还好说,如果是跨域的,父页面没有办法操作子页面,想要正确获取子页面高度的话,可以采用以下办法:

方法一:使用HTML5 postMessage

实现原理:子页面检测页面高度通过postMessage将值传给父页面

父页面: http://www.parent.com

<iframe src="http://www.children.com" frameborder="0" id="iframe" scrolling="no" width="100%"></iframe>
<script>
window.onload = function(){
window.addEventListener('message',function(event){
if(event.origin == "http://www.children.com") {
document.getElementById('iframe').style.height = event.data + "px";
}
})
}
</script>

子页面: http://www.children.com

window.onload = function () {
var h = document.body.scrollHeight;
parent.postMessage(h, "http://www.parent.com");
}

方法二:使用iFrame Resizer插件

iFrame Resizer插件会自动检测子页面的高度传给父页面,效果比较好不需要做过多的配置,强烈推荐此方案。

父页面: http://www.parent.com

<iframe src="http://www.parent.com/" frameborder="0" id="iframe" scrolling="no" width="100%"></iframe>
<script src="./iframeResizer.min.js"></script>
<script>
iFrameResize({log:true});
</script>

子页面: http://www.children.com

<script src="./iframeResizer.contentWindow.min.js"></script>

方法三:采用中转页面的方法

采用“迂回”的方式解决页面高度获取问题。在主页面的域下建一个空的页面,子页面引用这个空的页面,再通过传参的方式,将子页面的高度“告知”主页面,不推荐此方案

父页面:http://www.parent.com

<iframe src="http://www.children.com" frameborder="0" width="100%" id="iframe"></iframe>
<script type="text/javascript">
function updateIFrame(height) {
var iframe = document.getElementById('iframe-Scat');
iframe.setAttribute('height', height);
}
</script>

子页面: http://www.children.com

拿到子页面的高度后将值重新赋给父页面下的空页面

<iframe src="http://www.parent.com/blank.html" id="resize-iframe" style="display: none;"></iframe>
<script type="text/javascript">
window.onload = function() {
var h = document.body.scrollHeight;
document.getElementById("resize-iframe").src = "http://domain1.com/c.html?height=" + h;
}
</script>

空页面:http://www.parent.com/blank.html

因为此页面和父页面是同源,这时就可以直接把高度传给父页面

<script type="text/javascript">
window.onload = function() {
var h = location.search.replace('?', '').split('=')[1];
// parent.parent.document.getElementById("iframe-Scat").style.height = h + 'px';
window.top.updateIFrame(h);
}
</script>

跨域iframe如何实现高度自适应?的更多相关文章

  1. Iframe跨域JavaScript自动适应高度

    重点分析: 主域名页面:页面A,页面C 其它域名页面:页面B 步骤: 1.页面A(主域名)通过Iframe(id="iframeB")嵌套页面B(其它域名) 2.页面B(其它域名) ...

  2. 跨域iframe的高度自适应

    If you cannot hear the sound of the genuine in you, you will all of your life spend your days on the ...

  3. 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】

    Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的“BUG”就是iframe的高度无法自动适应,这一点让很多 ...

  4. 谷歌、火狐浏览器下实现JS跨域iframe高度自适应的完美解决方法,跨域调用JS不再是难题!

    谷歌.火狐浏览器下实现JS跨域iframe高度自适应的解决方法 导读:今天开发的时候遇到个iframe自适应高度的问题,相信大家对这个不陌生,但是一般我们都是在同一个项目使用iframe嵌套页面,这个 ...

  5. javascript跨域通信(一):利用location.hash实现跨域iframe自适应

    页面域关系: a.html所属域A:www.A.comb.html所属域B:www.B.com 问题本质: js对跨域iframe访问问题,因为要控制a.html中iframe的高度和宽度就必须首先读 ...

  6. 跨域iframe高度计算

    一.同域获取iframe内容 这里有两个细节: 1. 取iframe内的文档对象,标准浏览器使用contentDocument属性,IE低版本(IE6,7,8)使用document属性. 2. cal ...

  7. 使用postMesssage()实现跨域iframe页面间的信息传递----转载

    由于web同源策略的限制,当页面使用跨域iframe链接时,主页面与子页面是无法交互的,这对页面间的信息传递造成了不小的麻烦,经过一系列的尝试,最后我发现有以下方法可以实现: 1. 子页面url传参 ...

  8. [转载]跨域iframe高度自适应

    场景: 经常会有这样的需求,跟外部合作伙伴合作,要嵌入别人的页面,这时候就需要高度自适应了,在这种跨域的情况下如何解决呢? 解决: 在iframe(合作伙伴的页面,称为P页面)中创建一个隐藏的ifra ...

  9. 跨域iframe高度自适应(兼容IE/FF/OP/Chrome)

    采用JavaScript来控制iframe元素的高度是iframe高度自适应的关键,同时由于JavaScript对不同域名下权限的控制,引发出同域.跨域两种情况. 由于客户端js使用浏览器的同源安全策 ...

随机推荐

  1. Data Center手册(4):设计

    基础架构 拓扑图 Switching Path L3 routing at aggregation layer L2 switching at access layer L3 switch融合了三种功 ...

  2. Java提高篇(二):IO字节流、字符流和处理流

    在我们的Java语言当中,通常会有对文件进行读写,因此我们引入java的IO类来进行文件的读写. 一.字节流 下面是一个字节流的实例: import java.io.*; public class I ...

  3. linux 完全关闭tomcat

    由于直接调用tomcat的 shutdown.sh 有时无法完全关闭掉tomcat,使用 ps -ef | grep tomcat 查找发现tomcat依然还存在,并未完全关掉.在 catalina. ...

  4. #Java学习之路——基础阶段(第十篇)

    我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...

  5. [Swift]LeetCode566. 重塑矩阵 | Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  6. [Swift]LeetCode766. 托普利茨矩阵 | Toeplitz Matrix

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...

  7. [Swift]LeetCode1030. 距离顺序排列矩阵单元格 | Matrix Cells in Distance Order

    We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 & ...

  8. Java数据结构和算法 - 什么是2-3-4树

    Q1: 什么是2-3-4树? A1: 在介绍2-3-4树之前,我们先说明二叉树和多叉树的概念. 二叉树:每个节点有一个数据项,最多有两个子节点. 多叉树:(multiway tree)允许每个节点有更 ...

  9. Python--(爬虫与数据库的连接)

    (每一天都是属于你的!) Python对于初学后巩固基础的人还是更多的来接触python爬虫会更好一些,在Python爬虫中包含很多基础部分知识,并且在项目中会提升你的成功感!加油! 我在工作之余时间 ...

  10. spark Could not write all entries

    使用 spark 将 dataFrame 储存到 elasticsearch 出现如下报错: Caused by: org.elasticsearch.hadoop.EsHadoopException ...