window.postMessage()方法可以安全地实现Window对象之间的跨域通信。例如,在页面和嵌入其中的iframe之间。

不同页面上的脚本允许彼此访问,当且仅当它们源自的页面共享相同的协议,端口号和主机(也称为“同源策略”)。window.postMessage()提供了一个受控的机制相对来安全地规避这个限制。

发送消息的基本语法:

targetWindow.postMessage(message, targetOrigin, [transfer]);

targetWindow就是接收消息的窗口的引用。 获得该引用的方法包括:

  • Window.open
  • Window.opener
  • HTMLIFrameElement.contentWindow
  • Window.parent
  • Window.frames +索引值

message就是要发送到目标窗口的消息。 数据使用结构化克隆算法进行序列化。 这意味着我们可以将各种各样的数据对象安全地传递到目标窗口,而无需自己对其进行序列化。

targetOrigin就是指定目标窗口的来源,必须与消息发送目标相一致,可以是字符串“*”或URI。 *表示任何目标窗口都可接收,为安全起见,请一定要明确提定接收方的URI。

transfer是可选参数

接收端:

window.addEventListener("message", receiveMessage, false);
function receiveMessage(event){
if (event.origin !== "http://127.0.0.1:3808/")
return;
}
 
event对象有三个属性,分别是origin,data和source。event.data表示接收到的消息;event.origin表示postMessage的发送来源,包括协议,域名和端口;event.source表示发送消息的窗口对象的引用; 我们可以用这个引用来建立两个不同来源的窗口之间的双向通信。

完整示例:

1、父页面

  <title>father</title>
</head>
<body>
<div style="width: 200px;float: left;margin-right: 200px;border: 1px solid #333;">
<div id="color"> frame color</div>
</div>
<div>
<iframe id="child" src="http://127.0.0.1:3808/iframe-son/index.html"></iframe>
</div>
<script>
window.onload = function() {
// 初始化div颜色
window.frames[0].postMessage('getcolor', 'http://127.0.0.1:3808/');
} window.addEventListener('message',function(e) {
// 监听子页面颜色的改变即发送的消息,设置div颜色
var color = e.data;
document.getElementById('color').style.backgroundColor = color;
},false)
</script>
</body>
</html>

2、子页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>son</title>
</head>
<body>
<div id="container" onclick="changeColor();" style="width: 100%; height: 100%;background-color: rgb(204,102,0)">
click to change color
</div> <script>
var container = document.getElementById('container'); //监听父页面传递过来的信息
window.addEventListener('message', function (e) {
if(e.source != window.parent) return ;
// container.innerHTML = e.data var color = container.style.backgroundColor;
window.parent.postMessage(color,'*');
},false) //改变颜色的方法
function changeColor() {
var color = container.style.backgroundColor;
if(color=='rgb(204, 102, 0)'){
color='rgb(204, 204, 0)';
}else{
color='rgb(204,102,0)';
}
container.style.backgroundColor = color;
// 给父元素发送消息
window.parent.postMessage(color,'*');
}
</script>
</body>
</html>

最新文章

  1. DS 工作室
  2. SPSS数据分析—描述性统计分析
  3. POJ 1778 All Discs Considered(拓扑排序)
  4. HDU 4737 A Bit Fun 2013成都 网络赛 1010
  5. C程序设计语言练习题1-21
  6. andorid 开发笔记 -- 问题与解决
  7. html5 百分比计算
  8. php批量删除,批量操作
  9. 内嵌Tomcat的Connector对象的静态代码块
  10. CSS3对于盒中容纳不下的内容的显示——overflow属性
  11. (四十七)Quartz2D引擎初步
  12. LINUX磁盘分区
  13. Vue ElementUI 按需引入
  14. Python基于Python实现批量上传文件或目录到不同的Linux服务器
  15. ECMAScript 6 -- let和const命令
  16. OSI七层模式简单通俗理解
  17. Linux rhel7 无线网络配置
  18. 【转】字符编码笔记:ASCII,Unicode 和 UTF-8
  19. vnc server on Ubuntu
  20. 撩课-Web大前端每天5道面试题-Day22

热门文章

  1. Vuex 的使用 State Mutation Getter Action
  2. ISO/IEC 9899:2011 条款6.2——概念
  3. Git 代码撤销、回滚到任意版本(当误提代码到本地或master分支时)
  4. ES6深入浅出-11 ES6新增的API(上)-1.Object.assign
  5. 【Tomcat】Tomcat 基本使用(二)
  6. VMware虚拟机提示“锁定文件失败 打不开磁盘”解决方法
  7. 使用SampleRateConverter对音频采样率进行转换
  8. 软件测试成熟度模型TCMM (转载)
  9. bootstrap-table+Django: 服务端分页
  10. C# 文档注释规范