当前第2页 返回上一页
1 2 3 | window.onload= function (){
window.frames[0].postMessage( 'getcolor' , 'http://lslib.com' );
}
|
iframe接收消息,并把当前颜色发送给主页面呢
1 2 3 4 5 | window.addEventListener( 'message' , function (e){
if (e.source!=window.parent) return ;
var color=container.style.backgroundColor;
window.parent.postMessage(color, '*' );
},false);
|
主页面接收消息,更改自己p颜色
1 2 3 4 | window.addEventListener( 'message' , function (e){
var color=e.data;
document.getElementById( 'color' ).style.backgroundColor=color;
},false);
|
当点击iframe事触发其变色方法,把最新颜色发送给主页面
1 2 3 4 5 6 7 8 9 10 | 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, '*' );
}
|
主页面还是利用刚才监听message事件的程序处理自身变色
1 2 3 4 | window.addEventListener( 'message' , function (e){
var color=e.data;
document.getElementById( 'color' ).style.backgroundColor=color;
},false);
|
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
spring mvc+localResizeIMG实现H5端图片压缩上传
H5实现旋转立体魔方
以上就是postMessage实现跨域、跨窗口消息传递的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
h5里的postmessage api图文详解 详细介绍
html5跨域信息交互技术之postmessage代码实例详解
h5中怎样使用postmessage实现两个网页间传递数据
html5中postmessage api的基本使用
html页面跳转及参数传递问题
postmessage实现跨域、跨窗口消息传递
html怎样实现页面跳转时传递参数
html5通过postmessage进行跨域通信的方法
html5中的postmessage api基本使用方法分享
如何使用html5中postmessage实现ajax中的post跨域问题的详细介绍
更多相关阅读请进入《postmessage》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » postMessage实现跨域、跨窗口消息传递