跨子域的iframe高度自适应
一、跨子域的iframe高度自适应
比如 'a.jd.com/3.html' 嵌入了 'b.jd.com/4.html',这种跨子域的页面
3.html
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!DOCTYPE html><html> <head> <meta charset='utf-8' /> <title>1.html</title> <script type="text/javascript"> document.domain = 'jd.com' </script> </head> <body> <iframe id="ifr" src="b.jd.com/4.html" frameborder="0" width="100%"></iframe> </body></html> |
4.html
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>2.html</title> <script type="text/javascript"> document.domain = 'jd.com' </script> </head> <body> <p>这是一个ifrmae,嵌入在3.html里 </p> <p>根据自身内容调整高度</p> <p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><script> // 计算页面的实际高度,iframe自适应会用到 function calcPageHeight(doc) { var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight) var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight) var height = Math.max(cHeight, sHeight) return height } window.onload = function() { var height = calcPageHeight(document) parent.document.getElementById('ifr').style.height = height + 'px' }</script> </body></html> |
可以看到与同域引入对比,只要在两个页面里都加上document.domain就可以了
二、完全跨域的iframe高度自适应
分别有以下资源
- 页面 A:http://localhost:9001/A.html
- 页面 B:http://localhost:9001/B.html
- 页面 C:http://t.com:9001/C.html
大关系就是 A->C->B ,A与B同域名,B作为中间人,用地址栏传参形式,把C的高传出来
这四个资源有如下关系
1. A里嵌入C,A和C是不同域的,即跨域iframe
2. C里嵌入B,C和B是不同域的,但A和B是同域的
3. C里嵌入D.js,D.js放在和A同域的项目里
通过一个间接方式实现,即通过一个隐藏的B.html来实现高度自适应。
A.html
嵌入页面C:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>A.html</title>
</head>
<body>
<iframe id="ifr" src="http://t.com:9001/C.html" frameborder="0" width="100%"></iframe>
<script type="text/javascript"> </script> </body>
</html>
B.html
嵌入在C页面中,它是隐藏的,通过parent.parent访问到A,再改变A的iframe(C.html)高度,这是最关键的,因为A,B是同域的所以可以访问A的文档对象等。
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>B.html</title>
</head>
<body>
<script type="text/javascript">
window.onload = function() {
var isSet = false
var inteval = setInterval(function() {
var search = location.search.replace('?', '')
if (isSet) {
clearInterval(inteval)
return
}
if (search) {
var height = search.split('=')[]
var doc = parent.parent.document
var ifr = doc.getElementById('ifr')
ifr.style.height = height + 'px'
isSet = true
}
}, )
}
</script>
</body>
</html>
C.html
嵌入在A中,和A不同域,要实现C的自适应,C多高则A里的iframe就设为多高。C里嵌入B.html
<!doctype html>
<html>
<head>
<title>C.html</title>
<meta charset="utf-8">
</head>
<body>
<h3>这是一个很长的页面,我要做跨域iframe的高度自适应</h3>
<ul>
<li>页面 A:http://snandy.github.io/lib/iframe/A.html</li>
<li>页面 B:http://snandy.github.io/lib/iframe/B.html</li>
<li>页面 C:http://snandy.jd-app.com</li>
<li>D.js:http://snandy.github.io/lib/iframe/D.js</li>
</ul>
<p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p><p>A</p>
<iframe id="myifr" style="display:none" src="http://localhost:9001/B.html"></iframe> <script type="text/javascript">
// 计算页面的实际高度,iframe自适应会用到
function calcPageHeight(doc) {
var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)
var height = Math.max(cHeight, sHeight)
return height
}
window.onload = function() {
var doc = document
var height = calcPageHeight(doc)
var myifr = doc.getElementById('myifr')
if (myifr) {
myifr.src = 'http://localhost:9001/B.html?height=' + height
// console.log(doc.documentElement.scrollHeight)
}
};
</script>
</body>
</html>
跨子域的iframe高度自适应的更多相关文章
- JS跨域解决iframe高度自适应(IE8/Firefox/Chrome适用)
参考园友的js跨越实现,有提到三种方式: 1. 中间页代理方式,利用iframe的location.hash 参见:http://www.5icool.org/a/201203/a1129.html ...
- Iframe 高度自适应 example (跨子域实现)
跨子域的iframe高度自适应 比如 'https://www.kzwr.com/topics/baidu' 嵌入了 'http://pan.kzwr.com/',这种跨子域的页面,实现起来也比较简单 ...
- Iframe高度自适应(兼容IEFirefox、同域跨域)
在实际的项目进行中,很多地方可能由于历史原因不得不去使用iframe,包括目前正火热的应用开发也是如此. 随之而来的就是在实际使用iframe中,会遇到iframe高度的问题,由于被嵌套的页面长度不固 ...
- Iframe高度自适应(兼容IE/Firefox、同域/跨域)
在实际的项目进行中,很多地方可能由于历史原因不得不去使用iframe,包括目前正火热的应用开发也是如此. 随之而来的就是在实际使用iframe中,会遇到iframe高度的问题,由于被嵌套的页面长度不固 ...
- 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】
Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的“BUG”就是iframe的高度无法自动适应,这一点让很多 ...
- 跨域iframe高度自适应(兼容IE/FF/OP/Chrome)
采用JavaScript来控制iframe元素的高度是iframe高度自适应的关键,同时由于JavaScript对不同域名下权限的控制,引发出同域.跨域两种情况. 由于客户端js使用浏览器的同源安全策 ...
- 跨域iframe高度自适应(兼容IE/FF/OP/Chrome)
采用JavaScript来控制iframe元素的高度是iframe高度自适应的关键,同时由于JavaScript对不同域名下权限的控制,引发出同域.跨域两种情况. 由于客户端js使用浏览器的同源安全策 ...
- 谷歌、火狐浏览器下实现JS跨域iframe高度自适应的完美解决方法,跨域调用JS不再是难题!
谷歌.火狐浏览器下实现JS跨域iframe高度自适应的解决方法 导读:今天开发的时候遇到个iframe自适应高度的问题,相信大家对这个不陌生,但是一般我们都是在同一个项目使用iframe嵌套页面,这个 ...
- CSS完美实现iframe高度自适应(支持跨域)
Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的"BUG"就是iframe的高度无法自 ...
随机推荐
- 22 Maven高级应用
1.Maven基础知识回顾 maven是一个项目管理工具.依赖管理:maven对项目中的jar包的管理过程.传统的工程我们直接将jar包放置到项目中. maven工程真正的jar包放置在仓库中,项目中 ...
- SQL Server日志处理及安全访问
1.点SQL SERVER错误日志,右键,配置,限定错误日志的数目,比如6个 限制日志增长数量 2.然后运行命令: EXEC sp_cycle_errorlog ; 这个命令的作用是将当前日志归档,然 ...
- Windows 上的应用程序在运行期间可以给自己改名(可以做 OTA 自我更新)
原文:Windows 上的应用程序在运行期间可以给自己改名(可以做 OTA 自我更新) 程序如何自己更新自己呢?你可能会想到启动一个新的程序或者脚本来更新自己.然而 Windows 操作系统允许一个应 ...
- ASP.NET WebApi 学习与实践系列(2)---WebApi 路由请求的理解
目录 写在前面 WebApi Get 请求 1.无参数的请求 2.一个参数的请求 3.多个参数的请求 4.实体参数的请求 WebApi Post 请求 1.键值对请求 2.dynamic 动态类型请求 ...
- selenium中的元素操作之下拉列表操作(四)
下拉列表操作中分为两种:select.非select 1.非select的下拉框操作 非select下拉列表操作与网页元素操作一致,找到元素,定位元素,设置等待,点击元素等等 接下来操作百度的设置按钮 ...
- 神奇的外部嵌套(使用ROW_NUMBER()查询带条件的时候提示列名无效)
declare @pageIndex int -- 第几页 declare @pageSize int -- 每页包含的记录数 --这里注意一下,不能直接把变量放在这里,要用select select ...
- CSS-服务器端字体笔记
服务器端字体 在CSS3中可以使用@font-face属性来利用服务器端字体. @font-face 属性的使用方法: @font-face{ font-family:webFont; src:ur ...
- testNG helloWorld
1. 新建maven工程.File -> New -> Project -> Maven,不选Create from archetype,直接点击“Next”.GroupId和Art ...
- RSA算法二:迪菲赫尔曼公式变形
- java实现mysql数据备份
/** * @param hostIP ip地址,可以是本机也可以是远程 * @param userName 数据库的用户名 * @param password 数据库的密码 * @param sav ...