跨域iframe如何实现高度自适应?
经常有项目会要求实现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如何实现高度自适应?的更多相关文章
- Iframe跨域JavaScript自动适应高度
重点分析: 主域名页面:页面A,页面C 其它域名页面:页面B 步骤: 1.页面A(主域名)通过Iframe(id="iframeB")嵌套页面B(其它域名) 2.页面B(其它域名) ...
- 跨域iframe的高度自适应
If you cannot hear the sound of the genuine in you, you will all of your life spend your days on the ...
- 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】
Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的“BUG”就是iframe的高度无法自动适应,这一点让很多 ...
- 谷歌、火狐浏览器下实现JS跨域iframe高度自适应的完美解决方法,跨域调用JS不再是难题!
谷歌.火狐浏览器下实现JS跨域iframe高度自适应的解决方法 导读:今天开发的时候遇到个iframe自适应高度的问题,相信大家对这个不陌生,但是一般我们都是在同一个项目使用iframe嵌套页面,这个 ...
- javascript跨域通信(一):利用location.hash实现跨域iframe自适应
页面域关系: a.html所属域A:www.A.comb.html所属域B:www.B.com 问题本质: js对跨域iframe访问问题,因为要控制a.html中iframe的高度和宽度就必须首先读 ...
- 跨域iframe高度计算
一.同域获取iframe内容 这里有两个细节: 1. 取iframe内的文档对象,标准浏览器使用contentDocument属性,IE低版本(IE6,7,8)使用document属性. 2. cal ...
- 使用postMesssage()实现跨域iframe页面间的信息传递----转载
由于web同源策略的限制,当页面使用跨域iframe链接时,主页面与子页面是无法交互的,这对页面间的信息传递造成了不小的麻烦,经过一系列的尝试,最后我发现有以下方法可以实现: 1. 子页面url传参 ...
- [转载]跨域iframe高度自适应
场景: 经常会有这样的需求,跟外部合作伙伴合作,要嵌入别人的页面,这时候就需要高度自适应了,在这种跨域的情况下如何解决呢? 解决: 在iframe(合作伙伴的页面,称为P页面)中创建一个隐藏的ifra ...
- 跨域iframe高度自适应(兼容IE/FF/OP/Chrome)
采用JavaScript来控制iframe元素的高度是iframe高度自适应的关键,同时由于JavaScript对不同域名下权限的控制,引发出同域.跨域两种情况. 由于客户端js使用浏览器的同源安全策 ...
随机推荐
- Oracle 查询表空间使用情况
select * from (Select a.tablespace_name, to_char(a.bytes / 1024 / 1024, '99,999.999 ...
- Luogu P1381油滴扩展
传送门 数据范围给的很小啊,n >= 0 && n <= 7,所以给了DFS生存的空间. 对于每一个油滴,可以说在它下一个油滴放置之前,当前的这个油滴的半径并不确定(但是对 ...
- RevDebug -- VS 调试神器,你值得拥有!
1. What's RevDebug Don't debug - replay! Trace the root cause of bugs in a matter of seconds, save y ...
- HashMap 相关面试题及其解答
Q:HashMap 的数据结构? A:哈希表结构(链表散列:数组+链表)实现,结合数组和链表的优点.当链表长度超过 8 时,链表转换为红黑树. transient Node<K,V>[] ...
- Java实现桶排序和基数排序
桶排序代码: import java.util.Arrays; /** * 桶排序 * 工作的原理是将数组分到有限数量的桶里 * 每个桶再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序 ...
- Python学习宝典,Python400集让你成为从零基础到手写神经网络的Python大神
当您学完Python,你学到了什么? 开发网站! 或者, 基础语法要点.函数.面向对象编程.调试.IO编程.进程与线程.正则表达式... 当你学完Python,你可以干什么? 当程序员! 或者, 手写 ...
- [Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I
You are given a string representing an attendance record for a student. The record only contains the ...
- [Swift]LeetCode799. 香槟塔 | Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- [Swift]LeetCode838. 推多米诺 | Push Dominoes
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- Python面向对象中的类和对象
类和对象 目标 类和对象的概念 类和对象的关系 类的设计 01. 类和对象的概念 类 和 对象 是 面向对象编程的 两个 核心概念 1.1 类 类 是对一群具有 相同 特征 或者 行为 的事物的一个统 ...