Javascript中要实现跨域通信,主要有window.name,jsonp,document.domain,cors等方法。不过在H5中有一种新的方法postMessage可以安全实现跨域通信,并且使用简单。

要使用postMessage,首先得检查浏览器是否支持该方法,postMessage属于window对象,检测方法如下:

if('postMessage' in window){

}else{
console.log('浏览器不支持postMessage');
}

postMessage使用语法如下所示。

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

otherWindow必须是一个window对象的引用,如iframe的contentWindow,window.open的返回对象,window.frames[index]等。

targetOrigin指定otherWindow的源,如果目标窗口的协议,主机地址,端口只要一个不同,该方法便不会执行信息发送

为了能接收到postMessage发送的消息,必须在window对象上监听message事件,该事件对象包涵了data(消息)、origin(来源地址)、source(来源窗口代理)等属性,使用如下所示

window.onmessage = function(e){
if (e.origin === 'http://www.test.zmx.com') {
alert(e.data);
}
}

下面是使用postMessage的小例子,帮助理解。

http://www.test.zmx.com/postmessage.html,简称A页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<iframe frameborder="0" src="http://www.select.zmx.com/select.html"></iframe>
<input type="text" id="kw"><button id="btn">发送到子窗口</button>
<script type="text/javascript"> window.onload = function(){
window.onmessage = function(e) {
if (e.origin === 'http://www.select.zmx.com') {
alert("收到来自子窗口的消息:"+e.data);
}
}
btn.onclick = function(e) {
var val = kw.value;
window.frames[0].postMessage(val, 'http://www.select.zmx.com');
}
}
</script>
</body>
</html>

http://www.select.zmx.com/select.html,简称B页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简单下拉框插件</title>
<link rel="stylesheet" href="simpleSelect.css">
<style type="text/css">
body{ padding: 40px; }
</style>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="simpleSelect.js"></script>
</head>
<body>
<input type="text" id="in1"><button id="btn1">发送到父窗口</button>
<script>
window.onmessage = function(e){
if (e.origin === 'http://www.test.zmx.com') {
alert("收到来自父窗口的消息:"+e.data);
}
}
btn1.onclick = function(e) {
var val = in1.value;
window.top.postMessage(val, 'http://www.test.zmx.com');
}
</script>
</body>
</html>

在A页面中发送消息hello world给B页面,如下所示。

点击发送,则B页面则收到消息,如下所示。

B页面向A页面发送消息也是一样的,就不叙述了。

H5 Notes:PostMessage Cross-Origin Communication的更多相关文章

  1. H5 Notes:Navigator Geolocation

    H5的地理位置API可以帮助我们来获取用户的地理位置,经纬度.海拔等,因此我们可以利用该API做天气应用.地图服务等. Geolocation对象是我们获取地理位置用到的对象. 首先判断浏览器是否支持 ...

  2. 利用 pyhon 解决 Cross Origin Requests

    在学习 ajax 时遇到了一个问题 XMLHttpRequest cannot load file:xxxxxxxx . Cross origin requests are only supporte ...

  3. 跨域问题:Cross origin requests are only supported for protocol schemes: http...

    跨域:Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extensi ...

  4. Ajax本地跨域问题 Cross origin requests are only supported for HTTP

    问题:打开本地html文件时,报错如下 Cross origin requests are only supported for protocol schemes: http, data,chrome ...

  5. Blocking Cross Origin API request for /api/contents Creating Notebook Failed An error occurred while creating a new notebook.

    anacoda安装的jupyter,使用nginx进行了转发,远程访问可以进去,但是创建文件和创建目录都会报错 浏览器页面报错: 第一次使用jupyter创建python时错误:Creating No ...

  6. jquery读取本地文件,Windows上报错。XMLHttpRequest cannot load xxx. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.k.cors.a.c

    问题: 测试报告,使用本地的json.txt文件,结果文件读取失败,报错如下: XMLHttpRequest cannot load xxx. Cross origin requests are on ...

  7. Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-extension的问题

    Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-extension的问 ...

  8. 用临时用户数据目录启动Chrome,关闭安全检查等(解决Cross origin requests are only supported for HTTP?)

    Cross origin requests are only supported for HTTP? 参考:https://www.zhihu.com/question/20948649 批处理: s ...

  9. 【chrome错误】Cross origin requests are only supported for protocol schemes: http, data,chrome-extension, https, chrome-extension-reso

    使用ajax请求本地文件,chrome会报跨域错误. XMLHttpRequest cannot loadfile:///C:/Users/Li/Desktop/images/alist.json.C ...

随机推荐

  1. 设计爬虫Hawk背后的故事

    本文写于圣诞节北京下午慵懒的午后.本文偏技术向,不过应该大部分人能看懂. 五年之痒 2016年,能记入个人年终总结的事情没几件,其中一个便是开源了Hawk.我花不少时间优化和推广它,得到的评价还算比较 ...

  2. 【原】nodejs全局安装和本地安装的区别

    来微信支付有2年多了,从2年前的互联网模式转变为O2O模式,主要的场景是跟线下的商户去打交道,不像以往的互联网模式,有产品经理提需求,我们帮忙去解决问题. 转型后是这样的,团队成员更多需要去寻找业务的 ...

  3. Tcp/ip 报文解析

    在编写网络程序时,常使用TCP协议.那么一个tcp包到底由哪些东西构成的呢?其实一个TCP包,首先需要通过IP协议承载,而IP报文,又需要通过以太网传送.下面我们来看看几种协议头的构成 一 .Ethe ...

  4. 关于Layer弹出框初探

    layer至今仍作为layui的代表作,她的受众广泛并非偶然,而是这五年多的坚持,不断完善和维护.不断建设和提升社区服务,使得猿们纷纷自发传播,乃至于成为今天的Layui最强劲的源动力.目前,laye ...

  5. Hawk 5. 数据库系统

    Hawk在设计之初,就是以弱schema风格定义的.没有严格的列名和列属性.用C#这样的静态强类型语言编写Hawk,其实并不方便.但弱schema让Hawk变得更灵活更强大. 因此,Hawk虽然之前支 ...

  6. inline-block元素间距问题的几种解决方案

    不知道大家有没有碰到过设置了display:inline-block;的几个相邻元素之间有几px间距的问题,这里提供几种简单实用的解决方法,希望能够帮到大家!   方法1. 将<li>标签 ...

  7. 从netty-example分析Netty组件续

    上文我们从netty-example的Discard服务器端示例分析了netty的组件,今天我们从另一个简单的示例Echo客户端分析一下上个示例中没有出现的netty组件. 1. 服务端的连接处理,读 ...

  8. CSS3 @keyframes 动画

    CSS3的@keyframes,它可以取代许多网页动画图像,Flash动画,和JAVAScripts. CSS3的动画属性 下面的表格列出了 @keyframes 规则和所有动画属性: 浏览器支持 表 ...

  9. Android Weekly Notes Issue #237

    Android Weekly Issue #237 December 25th, 2016 Android Weekly Issue #237 这是本年的最后一篇issue, 感谢大家. 本期内容包括 ...

  10. svn常用命令

    1.新建版本库 [root@localhost repos]# mkdir -p project [root@localhost repos]# svnadmin create project [ro ...