跨域(五)——postMessage
HTML5的postMessage机制是客户端最直接的中档传输方法,一般用在iframe中父页与子页之间的客户端跨域通信。
浏览器支持情况:Chrome 2.0+、Internet Explorer 8.0+, Firefox 3.0+, Opera 9.6+, 和 Safari 4.0+ 。 Facebook已经使用了这个功能,用postMessage支持基于web的实时消息传递。
用法:otherWindow.postMessage(message, targetOrigin)
otherWindow: 对接收信息页面的window的引用。可以是页面中iframe的contentWindow属性;window.open的返回值;通过name或下标从window.frames取到的值。
message: 所要发送的数据,string类型。
targetOrigin: 用于限制otherWindow,“*”表示不作限制
下面是一个基于postMessage的简单聊天实例:
http://www.evil.com:8081/langtao/evil.html页面代码:
<iframe id="ifr" src="http://www.foo.com:8081/langtao/foo.html" width="500" height="300"></iframe>
<div id="msgbox">
</div>
<input type="text" id="msg" />
<input type="button" value="发送" id="btn"/>
<script type="text/javascript">
window.onload = function() {
document.getElementById('btn').onclick=function(){
var ifr = document.getElementById('ifr');
var targetOrigin = 'http://www.foo.com:8081/langtao/'; // 若写成http://www.foo.com:8081/langtao/foo.html'效果一样
var msg=document.getElementById('msg').value;
ifr.contentWindow.postMessage(msg,targetOrigin); var c=document.createElement('div');
c.innerHTML="你说:"+msg;
msgbox.appendChild(c);
}
};
window.addEventListener('message', function(e){
// 通过origin属性判断消息来源地址
if (e.origin == 'http://www.foo.com:8081') {//不可以设置为'http://www.foo.com:8081/'
var c=document.createElement('div');
c.innerHTML="来自"+e.origin+"的消息:"+e.data;
msgbox.appendChild(c);
}
}, false);
</script>
http://www.foo.com:8081/langtao/foo.html页面代码:
<div id="msgbox">
</div>
<input type="text" id="msg" />
<input type="button" value="发送" id="btn"/>
<script type="text/javascript">
var msgbox=document.getElementById('msgbox');
window.addEventListener('message', function(e){
if (e.origin == 'http://www.evil.com:8081') {
var c=document.createElement('div');
c.innerHTML="来自"+e.origin+"的消息:"+e.data;
msgbox.appendChild(c);
}
}, false);
document.getElementById('btn').onclick=function(){
var targetOrigin = 'http://www.evil.com:8081/langtao/';
var msg=document.getElementById('msg').value;
window.top.postMessage(msg, targetOrigin); var c=document.createElement('div');
c.innerHTML="你说:"+msg;
msgbox.appendChild(c);
}
</script>
效果图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgcAAAG6CAIAAAATf3JaAAAgAElEQVR4nO3d/XcU153n8fp75szOnpOTzIzJZGd2xzOTTeJsPHESe5wwHgeTjBM7OI6Mx7EdxvbaoAkDhE3O2DHBWMY8W0INAgwYDAYLYx4kxKMQAknoodWtbvWjurqV3h/KrlvUw61b1c/i/Trf49NdffvWrVL1/VRVC0ubSukURVEUZZQ2Oaur1NhM/uJw9OMLwx+cvrz/RH/P8b6e4337T/QfOX3l4wvDF4ejYzN5xa4oiqKopi3/VBieSp3sv7Hv2Pn+wdFoIpPO63P6fKE4XyjOz+nz6bw+NZPpHxzdd+z8xxeGb0bTDd8kiqIoKnTJUuF2Yu7UxVsHTlwYiybnivN6cX6uWJorlHKWmiuU5oolvTg/V5wfiyYPHO/75NLIeKLQ8A2jKIqiQpQ2Mau71nA0/X7vxSvDk3N6aa44ny2UfGuuOJ8vlC4PT35w+spILHc7MefVOUVRFNWcpU0kdWcNTaTePzkwncwWivPZuVJGubJzpUJxfjqZ3X9yYPv+j3cfOdN99CxFURTVKqWNJ3VbDUcz+z7qn0nl8vq8eh5YK6/Pz6TzJ88MZHP5EgCgddhTYSSe339yYDKezuvz6blS6Mrr88lU+syFK6X5+TIAoEXYU6F34Nal4cm8Pp/OlyqsYmn+1vjUzdGJRm8jAECVdjupmzU0ld53ciAzV0zPlVJ5n3p763vv7uyWNEjPlQrF0qf9lwp6sdGbCQBQot1O6Gad6Lt5ayKRmZufzZXk9dKqtf/9L/7mz/78b377+7clzXKF+fjM7PWbY16rz+VyFy9e/OCDDyKRSCQS+eCDDy5evJjL5eq5CwAAJm0soRt1M5bv/vB8KleczZWS0np51dq//J9f/8Z3/+VrDyz+4l/9XSJb9Go5myvl9dLHZwaKpZJz3WNjY3v27Dl58uTZs2cv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q3zaXHTO++fiO7gJO3/K7Rq7NuJsUlG8qZLPZK340TZM8dS63NvBqbJPNZkkFoOECf69gm/edc3eguz3VbQZXKqlwSUrTNOtj46n5wNnSa7kcqQA0g5B3kJwLnafzXjeRbG2URkkqVMA3FTKZzIAC4+clWWg8cP3pu77XJpPJkApAw/F/x1v4VFKhvwmQCkAzIBUWPt9USKfTfU0gnU6TCkDDkQoLn+/fVxgZGYnFYucaKhaLjYyM8PcVgIbjb7EtcL5/i629vX3NmjVjY2PphhobG1uzZk07f4sNaDT3VMhk8qNTCWphVCaTlx8EK1eubGtrq8dfZ/bW1ta2cuXKGhzhAILhd3sAAAKpAAAQSAUAgEAqAAAEUgEAIJAKAACBVAAACKQCAED4/3/DVU0fBeWRAAAAAElFTkSuQmCC" alt="" />
从代码中可以看到,postMessage机制,这种跨域需要双方默契配合,且可以在客户端通过orgin进行判断请求的来源是否合法。
跨域(五)——postMessage的更多相关文章
- 跨域传输信息postMessage
widnow.postMessage()方法允许安全的跨域传输. Syntax otherWindow.postMessage(message, targetOrigin, [transfer]); ...
- JavaScript 跨域:window.postMessage 实现跨域通信
JavaScript 跨域方式实现方式有很多,之前,一篇文章中提到了 JSONP 形式实现跨域.本文将介绍 HTML5 新增的 api 实现跨域:window.postMessage . 1 othe ...
- 详解JS跨域问题
什么是跨域? 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. JavaScript 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 【前端】【转】JS跨域问题总结
详情见原博客:详解js跨域问题 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 跨域资源共享(CORS) CORS(Cross-Origin Resource Sharing)跨域资源 ...
- js跨域问题解决方案
跨域:当协议.域名.端口号任何一个不相同时,叫称为跨域. HTML5 CORS(cross-origin-resource-sharing)跨域资源共享: 原理:当需要访问跨域的资源时,可以通 ...
- 详解js跨域
什么是跨域? 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 对于端口和协议的不同,只能通过后台来解决.URL 说明 是否允许通信 http://www.a.com/a.js http ...
- JS中的跨域问题
一.什么是跨域? 1.定义:跨域是指从一个域名的网页去请求另一个域名的资源.比如从www.baidu.com 页面去请求 www.google.com 的资源.但是一般情况下不能这么做,它是由浏览器的 ...
- js跨域解决方式
什么是跨域? 概念:仅仅要协议.域名.port有不论什么一个不同,都被当作是不同的域.(所谓同源是指,域名.协议,port同样.),对于port和协议的不同,仅仅能通过后台来解决. URL 说明 是否 ...
- 在javascript中的跨域解决
跨域产生的原因 跨域是由浏览器的同源策略引起的,即不同源(协议,域名,端口中其中有一个不同)的js是不能读取对方的资源的.当要网站中的js要请求其他网站的数据时就会产生跨域问题,就像下面这样,浏览器会 ...
- 一个iOS程序员眼中的跨域问题
摘要: 跨域问题是web开发领域一个常见的问题,相信每个web开发者都遇到"跨域"的问题 最近公司的iOS开发任务比较少,所以自己最近开始了Web开发的任务,在用H5做了很多页面, ...
随机推荐
- VRRP、Track与NQA联动配置举例(Master监视上行链路)
原文 http://www.h3c.com/cn/d_201708/1018729_30005_0.htm#_Toc488338729 1. 组网需求 Host A需要访问Internet上的Host ...
- SqlServer全表遍历
DECLARE @temp TABLE ( , ) , ) ) DECLARE @tempId INT , ) INSERT INTO @temp VALUES ( 'a' ) INSERT INTO ...
- python的json模块介绍
转载:https://blog.csdn.net/xsj_blog/article/details/51921664 对于数据传递方面,XML是一种选择,还有一种选择是JSON,它是一种轻量级的数据交 ...
- Hive与HBase集成进行数据分析
我们把hive的安装包上传的节点3来 解压 现在我们还是老规矩通过notopad++来连接我们的虚拟机来配置文件,把下面这两个文件重命名一下 修改这个文件 对hive-env.sh我们修改这里 下面我 ...
- Android CoordinatorLayout实现多列表切换并和头布局联动;
注意:不是双列表联动,是多列表和头布局联动: 大概就是和饿了么店铺首页类似的布局框架吧,头布局显示时,列表RecyclerView或ScrollView和头布局一起滚动,头布局完全隐藏后列表再去滚动, ...
- WebView加载失败或网络异常时,替换WebView的错误界面;
WebView在加载失败时会显示一个失败原因的界面,各个手机显示的界面还都不一样,部分手机还会把Url显示出来:我们要做的就是统一加载失败的界面: 大概思路:在WebView这个控件上面再覆盖一个Vi ...
- Ddr2,ddr3,ddr4内存条的读写速率
理论极限值是可以计算的:1333MHz * 64bit(单通道,双通道则128bit) / 8(位到字节单位转换) = 10.664GB/s.这只是理论,实际发挥还要看内存控制器,实际上1333单条跑 ...
- Spring获取application.properties
方法一:@Value获取属性值 首先在application.properties中添加属性值 app.name=MyApp app.description=${app.name} is a Spri ...
- 在html中使用thymeleaf编写通用模块
在编写页面时,常常会需要用到通用模块,比如header部分.footer部分等. 项目前端使用的是themeleaf模板引擎,下面简单介绍下使用themeleaf写header通用模块: 1. 通用部 ...
- 初识vuejs
转行前端,时间也不短了,也见识到了前端行业的蓬勃发展,以及一些新鲜技术的层出不穷. 由于自身计算机基础的薄弱,更加上一直没有遇上一个公司力推新技术,所以一直以来基本上都是靠着jquery和则zepto ...