跨域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使用浏览器的同源安全策 ...
随机推荐
- js-day03-事件响应和练习题
DOM事件编程 事件驱动编程:所谓事件驱动,简单地说就是你点什么按钮(即产生什么事件),电脑执行什么操作(即调用什么函数).当然事件不仅限于用户的操作. 当对象处于某种状态时,可以发出一个消息通知,然 ...
- tag cloud的相关资料
http://reverland.org/python/2013/01/28/visualize-your-shell-history/ https://github.com/reverland/sc ...
- Lesson 25 Do the English speak English?
Text I arrived London at last. The railway station was big, black and dark. I did not know the way t ...
- 在上线项目中,用Vue写一个星级评价
先看一下效果: html: <div class="big-star-box"> <img :src="imgNum>0 ? srcStar : ...
- Win10 安装 VMWare中 MAC OS X的安装,VMWare tools的配置与iOS的Helloworld
iOS的开发必须在MAC OS X系统下进行,这很蛋疼,现在MACBOOK动不动就上千上万大洋,这足够买台配置怪兽了,好吗?然而,我们是可以通过在VMWare中安装MAC OS X进行iOS开发的.对 ...
- SDL 开发实战(七): 使用 SDL 实现 PCM播放器
在上文,我们做了YUV播放器,这样我们就入门了SDL播放视频.下面我们来做一个PCM播放,即使用SDL播放PCM数据. 下面说明一下使用SDL播放PCM音频的基本流程,主要分为两大部分:初始化SDL. ...
- [Swift]LeetCode214. 最短回文串 | Shortest Palindrome
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [Swift]LeetCode547. 朋友圈 | Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- [Swift]LeetCode560. 和为K的子数组 | Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- CoCos2dx开发:PC端调试运行正常但打包apk文件后在手机上点击闪退
记:今天调试时出现的一个PC端调试运行正常,但打包apk文件后在手机上点击闪退的问题. 可能在不同的情况条件下,会有不同的原因导致apk安装后闪退问题.拿android studio等软件来说,开发安 ...