html中跨页面通讯方式,[转]html5: postMessage解决跨域和跨页面通讯的问题

平时作web开发的时候关于消息传递,除了客户端与服务器传值,还有几个常常会遇到的问题:html

多窗口之间消息传递(newWin = window.open(..));

页面与嵌套的iframe消息传递

postMessage方法

postMessage是html5引入的API能够更方便、有效、安全的解决这些问题。postMessage()方法容许来自不一样源的脚本采用异步方式进行有限的通讯,能够实现跨文本档、多窗口、跨域消息传递。html5

postMessage(data,origin)方法接受两个参数

data:要传递的数据,

html5规范中提到该参数能够是JavaScript的任意基本类型或可复制的对象,然而并非全部浏览器都作到了这点儿,部分浏览器只能处理字符串参数,因此咱们在传递参数的时候须要使用JSON.stringify()方法对对象参数序列化,在低版本IE中引用json2.js能够实现相似效果。web

origin:字符串参数,指明目标窗口的源,

协议+主机+端口号[+URL],URL会被忽略,因此能够不写,这个参数是为了安全考虑,someWindow.postMessage()方法只会在someWindow所在的源(url的protocol, host, port)和指定源一致时才会成功触发message event,固然若是愿意也能够将参数设置为"*",someWindow能够在任意源,若是要指定和当前窗口同源的话设置为"/"。json

MessageEvent的属性

data:顾名思义,是传递来的message

source:发送消息的窗口对象

origin:发送消息窗口的源(协议+主机+端口号)

示例:

同域父子页面间通信

父页面a.html:跨域

//> localhost:9011/a.html

page A

hello world

post message

functionsend() {var data = document.querySelector('#data').value;//注意: 只会触发当前window对象的message事件

//也能够访问子页面的window对象,触发子页面的message事件 (window.frames[0].postMessage(...))

//window.postMessage(data, '/');

//data = {name: 'sandy', age: 20, fav: {sing: true, shop: false}}; // 也能够传普通对象

window.frames[0].postMessage(data, '/'); //触发同域子页面的message事件

//window.frames[0].postMessage(data, 'http://localhost:9022/'); // 触发跨域子页面的messag事件

}//当前页面执行 window.postMessage(..)//或//子页面执行 parent.postMessage(...) 都会触发下面的回调, messageEvent.source不一样而已

window.addEventListener('message', function(messageEvent) {var data = messageEvent.data;//messageEvent: {source, currentTarget, data}

console.info('message from child:', data);

},false);

子页面b.html浏览器

//> localhost:9011/b.html

page B

send

var data =ev.data;

console.info('message from parent:', data);

},false);functionsend() {var data = document.querySelector('#inp').value;//window.postMessage(data, '*'); // 触发当前页面的message事件

parent.postMessage(data, '*'); //触发父页面的message事件

//parent.postMessage(data, 'http://localhost:9011/'); // 若父页面的域名和指定的不一致,则postMessage失败

}

跨域父子页面间通信

父页面a.html:安全

//> localhost:9011/a.html

page A

hello world

post message

functionsend() {var data = document.querySelector('#data').value;

window.frames[0].postMessage(data, 'http://localhost:9022/'); //触发跨域子页面的messag事件

}

window.addEventListener('message', function(messageEvent) {var data =messageEvent.data;

console.info('message from child:', data);

},false);

子页面b.html服务器

//> localhost:9022/b.html

page B

send

var data =ev.data;

console.info('message from parent:', data);

},false);functionsend() {var data = document.querySelector('#inp').value;

parent.postMessage(data,'http://localhost:9011/'); //若父页面的域名和指定的不一致,则postMessage失败

//parent.postMessage(data, '*'); // 触发父页面的message事件

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值