解决Iframe跨域高度自适应,利用window.postMessage()实现跨域消息传递页面高度(JavaScript)
在iframe跨域引用高度自适应这块写的js方式都试了不管用,最终使用的是window.postMessage() 跨域获取高度 传递信息
1.首先,在主页面上使用iframe引入子页面:也就是A.html页面引入B.html页面,下面看看A.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<style>
*{
padding: 0;
margin: 0;
}
</style>
<body>
<div>Hello! Is A.Html</div> <div align=center>
<iframe Id="frame_child" width="100%"></iframe>
</div>
</body>
</html>
<script>
$(function() {
window.onload = function() {
var url = "http://liting397.cn/"; //要访问子类调用地址
$("#frame_child").attr("src", url);
$("#frame_child").load(function() {
setTimeout(function() {
var frame = document.getElementById("frame_child").contentWindow;
var message = {
parentOrigin: window.origin,
msg: "收到请回复"
};
frame.postMessage(JSON.stringify(message), url);
console.log('发送成功');
}, 2000);
window.addEventListener("message", receiveMessage, false);
});
}
var receiveMessage = function(event) {
//if (event.origin !== event.data.Domain)
// return;
console.log("子页面有消息来了");
$("#frame_child").attr("height", event.data + 'px');
window.removeEventListener("message", receiveMessage, false);
}
})
</script>
2.子页面代码块js下边是B.html页面,B.html页面

<script>
//本ifram为自适应js 使用postMessage 像父类传递消息
$(function() {
window.addEventListener('message', function(event) {
const data = JSON.parse(event.data)
if (event.origin !== data.parentOrigin) {
return
}
event.source.postMessage(document.body.scrollHeight, event.origin);
}, false);
});
</script>
这里是写好的ifram引用地址 可以参考 http://liting400.cn/
解决Iframe跨域高度自适应,利用window.postMessage()实现跨域消息传递页面高度(JavaScript)的更多相关文章
- HTML5中window.postMessage,在两个页面之间的数据传递
HTML5中window.postMessage,在两个页面之间的数据传递 2015年11月3日 8536次浏览 关于postMessage window.postMessage虽然说是html5的功 ...
- Iframe高度自适应(兼容IEFirefox、同域跨域)
在实际的项目进行中,很多地方可能由于历史原因不得不去使用iframe,包括目前正火热的应用开发也是如此. 随之而来的就是在实际使用iframe中,会遇到iframe高度的问题,由于被嵌套的页面长度不固 ...
- iframe 跨域问题解决方案 利用window.name+iframe跨域获取数据详解
详解 前文提到用jsonp的方式来跨域获取数据,本文为大家介绍下如何利用window.name+iframe跨域获取数据. 首先我们要简单了解下window.name和iframe的相关知识.ifra ...
- 利用HTML5的window.postMessage实现跨域通信
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp77 HTML5的window.postMessage简述 postM ...
- 利用html5 postMessage接口跨域设置iframe大小
<!doctype html> <html> <head> <title>Document A</title> <meta chars ...
- JavaScript 跨域:window.postMessage 实现跨域通信
JavaScript 跨域方式实现方式有很多,之前,一篇文章中提到了 JSONP 形式实现跨域.本文将介绍 HTML5 新增的 api 实现跨域:window.postMessage . 1 othe ...
- window.postMessage()实现跨域消息传递
window.postMessage() 方法可以安全地实现跨源通信.通常,对于两个不同页面的脚本,只有当执行它们的页面位于具有相同的协议(通常为https), 端口号(443为https的默认值), ...
- 使用window.postMessage实现跨域通信
JavaScript由于同源策略的限制,跨域通信一直是棘手的问题.当然解决方案也有很多: document.domain+iframe的设置,应用于主域相同而子域不同: 利用iframe和locati ...
- [转载]跨域iframe高度自适应
场景: 经常会有这样的需求,跟外部合作伙伴合作,要嵌入别人的页面,这时候就需要高度自适应了,在这种跨域的情况下如何解决呢? 解决: 在iframe(合作伙伴的页面,称为P页面)中创建一个隐藏的ifra ...
随机推荐
- [Centos7]Centos7安装
Centos7安装 安装wget yum -y install wget 更换镜像源 # 备份本地yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum. ...
- hdu 5761 Rower Bo 微分方程
Rower Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- 日照学习提高班day3测试
A 思路: 一看到'#''.'什么的就想到搜索怪我怪我... 这道题勉强说是搜索别打我qwq 1)因为不重复,所以首先要判断是否%5==0,若不满足,直接输出NO 2)弄个vis数组记录是否被搜过,如 ...
- java中MD5函数
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5U ...
- A. Sea Battle
A. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Hive运行原理--JOIN
对于 JOIN 操作: INSERT OVERWRITE TABLE pv_users SELECT pv.pageid, u.age FROM page_view pv JOIN user u ON ...
- 20191114-4 Beta发布用户使用报告
20191114-4 Beta发布用户使用报告 此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/10007 组名:胜利点 组 ...
- SpringMVC——Servlet容器启动时初始化SpringMVC应用的原理
在 Servlet 3.0标准中含有一个 ServletContainerInitializer接口,所有实现了这个接口的类会在容器启动的时候得到一个通知,并且会调用其 onStartup()方法,这 ...
- 一、基础篇--1.2Java集合-HashMap和HashTable的区别
HashMap和HashTable的区别 1.继承的父类不同,HashMap继承的是AbstractMap类,HashTable继承的是Dictionary类,不过都实现了Map.Clone.Seri ...
- Memcached 在Windows和Linux的安装和使用
Memcached 把经常操作的数据导入到内存中 Memcached是一个高性能的支持分布式的内存存储系统,可以看成一个巨大的hash表.形式:key->value key(唯一键值string ...