iframe高度自适应

摘要:
前两天在网上看到了一道面试题,问iframe高度自适应的问题。发现自己之前几乎没有关注过iframe的问题,所以在这里记录一下。父页面取到参数后再给iframe高度赋值。variframeHeight=function(){varhash=window.location.hash.slice;if{iframe.height=hash.replace;}setTimeout;};iframeHeight();可以参考:小tip:iframe高度动态自适应这里思考了一下是不是可以不写死页面的地址:假设面试题目中提到的页面A:www.taobao.com内部嵌入页面B:www.tmall.com页面,要让B页面高度自适应的解决方案参考各种资料,可以利用中间代理页面agent.html来完成。

前两天在网上看到了一道面试题,问iframe高度自适应的问题。发现自己之前几乎没有关注过iframe的问题,所以在这里记录一下。

原题目是: 页面A的域名是:http://www.taobao.com,页面B的域名是http://www.tmall.com,如果A使用iframe引用页面B,如何做到iframe的高度自适应(即B内容有多高,iframe就有多高)

在这里首先分析一下如果不涉及跨域或者只是主域相同,子域不同的情况下的解决方案:

父页面代码:

1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4     <meta http-equiv="Content-Type"content="text/html"charset="UTF-8">
5     <title>Document</title>
6 </head>
7 <body>
8 <iframe id="iframe1"name="iframe1"src="http://www.iframe.com"frameborder="0"style="100%"></iframe>
9     <script>
10         //document.domain = "iframe.com"; 主域相同,子域不同
11         functionsetIframe(childrenIF) {
12 
13             //contentWindow 兼容各个浏览器,可取得子窗口的 window 对象。
14             //contentDocument Firefox 支持,> ie8 的ie支持。可取得子窗口的 document 对象。
15             varchildrenWin =childrenIF.contentWindow ||childrenIF.contentDocument.parentWindow;
16             if(childrenWin.document.body) {
17                 //document.documentElement返回文档对象(document)的根元素(例如,HTML文档的 <html> 元素)。
18 childrenIF.height =childrenWin.document.documentElement.offsetHeight ||childrenWin.document.body.offsetHeight;
19 }
20 
21 }
22 window.onload = function() {
23 setIframe(document.querySelector('#iframe1'));
24 }
25     </script>
26 </body>
27 </html>

看到张鑫旭的博客里说到另一种方法,是在iframe页面传递一个参数给父页面,告知其高度。父页面取到参数后再给iframe高度赋值。

大致原理在子页面iframe里定义

// 为了防止window.location.hash产生跨域问题,可以直接写死hostUrl地址:利用window.top.location = 父页面地址(写死) + 锚点
var hostUrl = window.location.hash.slice(1); hostUrl += "#height=" + 1294; window.top.location = hostUrl;

然后将子页面嵌入到父页面中,父页面提取location中的height数值,从而更改iframe高度。

var iframeHeight = function() {    
    var hash = window.location.hash.slice(1);
    if (hash && /height=/.test(hash)) {
        iframe.height = hash.replace("height=", "");
    }
    setTimeout(iframeHeight, 200);
};
iframeHeight();

可以参考:小tip:iframe高度动态自适应

这里思考了一下是不是可以不写死页面的地址:

假设面试题目中提到的页面A:www.taobao.com内部嵌入页面B:www.tmall.com页面,要让B页面高度自适应的解决方案

参考各种资料,可以利用中间代理页面agent.html来完成。

主要原理是agent页面和A页面同源,将agent页面嵌入到B页面获取到B页面宽高后,通过url传递宽高值。通过agent来操作A页面中嵌入的B页面宽高。

1. 在A(taobao)页面嵌入iframe

<iframe src="http://www.tmall.com"id="Iframe"frameborder="0"scrolling="no"style="border:0px;"></iframe>

2. 在B(tmall)页面嵌入agent_iframe,获取B页面的宽高。(将获取到的宽高通过url传递)

<iframe id="agent_iframe"height="0"width="0"src="http://www.taobao.com/agent.html"style="display:none" ></iframe>
<script type="text/javascript">(functionautoHeight(){
        vartmall_width =Math.max(document.body.scrollWidth,document.body.clientWidth);
        vartmall_height =Math.max(document.body.scrollHeight,document.body.clientHeight);
        varagent_iframe =document.getElementById("agent_iframe");
        //这里通过hash传递tmall的宽高
agent_iframe.src =agent_iframe.src + "#" +tmall_width + "|" +tmall_height;  
    })();
</script>     

3. 在agent.html插入代码(因为agent和A页面是相同域名,所以可以通过子元素来控制父元素的父元素[因为agent是嵌入在B页面的,B页面嵌入在A页面,因此agent可以控制A页面的元素,此处为多层嵌套,有点绕]的宽高)

<script type="text/javascript">
    var tmall_iframe = window.parent.parent.document.getElementById("Iframe");
    var hash_url =window.location.hash;
    if (hash_url.indexOf("#")>=0){
        var hash_width = hash_url.split("#")[1].split("|")[0]+"px";
        var hash_height = hash_url.split("#")[1].split("|")[1]+"px";
        tmall_iframe.style.width =hash_width;
        tmall_iframe.style.height =hash_height;
    }
</script>

总结

个人认为,如果父页面的地址是固定的,我觉得直接写死地址是比较方便直观的方法。当然还有很多其他方法可以实现高度自适应。

详见:iframe高度自适应的6个方法

免责声明:文章转载自《iframe高度自适应》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Opengrok服务器搭建step by step负载均衡和冗余技术下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

新手入门贴:史上最全Web端即时通讯技术原理详解

 关于IM(InstantMessaging)即时通信类软件(如微信,QQ),大多数都是桌面应用程序或者native应用较为流行,而网上关于原生IM或桌面IM软件类的通信原理介绍也较多,此处不再赘述。而web端的IM应用,由于浏览器的兼容性以及其固有的“客户端请求服务器处理并响应”的通信模型,造成了要在浏览器中实现一个兼容性较好的IM应用,其通信过程必然是...

js获取iframe的parent对象

使用谷歌浏览器调试代码时无意间发现了一个奇特的问题:从iframe页面调用父级页面的方法,window.parent.text(),出现 Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports mu...

flex自适应高度内容高度超出容器高度自动出现滚动条的问题

在容器中设置 flex-grow:2; overflow-y:auto;overflow-x:hidden;容器高度自适应。 内容高度不固定,无法出现滚动条,然后在容器中添加height:0,出现滚动条,个人猜想为设置height:0后,将默认的盒子模型高度设置为空,让flex设置的高度生效; //TODO 实验,查资料...

webstorm下的sass自动编译和移动端自适应实践

1、安装Ruby 2、安装sass 3、webstorm配置file watcher 4、移动端自适应 1、安装Ruby   安装Ruby,有多种方式,打开官网下载 因为,使用的是window选择RubyInstall,下载地址 RubyInstall下载地址 选择对应系统的版本,下载完成,安装 添加到path,建议勾上,安装完成后,打开开始面板,...

css3 flex流动自适应响应式布局实例 转

转自:http://www.tuicool.com/articles/auEbMzU 感谢他的分享, 一、图片自适应居中 实例图: 实例HTML: <div class="demo"> <img src="http://dummyimage.com/100x100" alt=""> </div> <div c...

vue使用iframe嵌入html,js方法互调

前段时间 使用h5搞了个用cesium.js做的地图服务功能,后来想整合到vue项目,当然最简单的就是iframe直接拿来用了。但html和vue的方法交互就是成了问题,vue调用html种方法还好,尤其是html调用vue中的方法当初就没有解决,忙着项目上线直接搞了个setInterval不停轮询,哎不说他了;现在空点了来把问题解决了,俗话说得好闲时学来...