• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >html5 > postMessage实现跨域、跨窗口消息传递

postMessage实现跨域、跨窗口消息传递

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-03

本文主要包含postMessage,传递,消息等相关知识,匿名希望在学习及工作中可以帮助到您
这次给大家带来postMessage实现跨域、跨窗口消息传递,postMessage实现跨域、跨窗口消息传递的注意事项有哪些,下面就是实战案例,一起来看一下。

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

1.页面和其打开的新窗口的数据传递

2.多窗口之间消息传递

3.页面与嵌套的iframe消息传递

4.上面三个问题的跨域数据传递

postMessage()

这些问题都有一些解决办法,但html5引入的message的API可以更方便、有效、安全的解决这些难题。postMessage()方法允许来自不同源的脚本采用异步方式进行有限的通信,可以实现跨文本档、多窗口、跨域消息传递。

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

1.data:要传递的数据,html5规范中提到该参数可以是JavaScript的任意基本类型或可复制的对象,然而并不是所有浏览器都做到了这点儿,部分浏览器只能处理字符串参数,所以我们在传递参数的时候需要使用JSON.stringify()方法对对象参数序列化,在低版本IE中引用json2.js可以实现类似效果。

2.origin:字符串参数,指明目标窗口的源,协议+主机+端口号[+URL],URL会被忽略,所以可以不写,这个参数是为了安全考虑,postMessage()方法只会将message传递给指定窗口,当然如果愿意也可以建参数设置为"*",这样可以传递给任意窗口,如果要指定和当前窗口同源的话设置为"/"。

http://test.com/index.html

<p style="width:200px; float:left; margin-right:200px;border:solid 1px #333;">
        <p id="color">Frame Color</p>
    </p>
    <p>
        <iframe id="child" src="http://lsLib.com/lsLib.html"></iframe>
 </p>

我们可以在http://test.com/index.html通过postMessage()方法向跨域的iframe页面http://lsLib.com/lsLib.html传递消息

window.onload=function(){
            window.frames[0].postMessage('getcolor','http://lslib.com');
        }

接收消息

test.com上面的页面向lslib.com发送了消息,那么在lslib.com页面上如何接收消息呢,监听window的message事件就可以

http://lslib.com/lslib.html

window.addEventListener('message',function(e){
                if(e.source!=window.parent) return;
                var color=container.style.backgroundColor;
                window.parent.postMessage(color,'*');
            },false);

这样我们就可以接收任何窗口传递来的消息了,为了安全起见,我们利用这时候的MessageEvent对象判断了一下消息源,MessageEvent是一个这样的东东

有几个重要属性

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

2.source:发送消息的窗口对象

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

这样就可以接收跨域的消息了,我们还可以发送消息回去,方法类似

简单的demo

在这个例子中,左边的p会根据右边iframe内p颜色变化而变化

<!DOCTYPE html>
<html>
<head>
    <title>Post Message</title>
</head>
<body>
    <p style="width:200px; float:left; margin-right:200px;border:solid 1px #333;">
        <p id="color">Frame Color</p>
    </p>
    <p>
        <iframe id="child" src="http://lsLib.com/lsLib.html"></iframe>
    </p>
    <script type="text/javascript">
        window.onload=function(){
            window.frames[0].postMessage('getcolor','http://lslib.com');
        }
        window.addEventListener('message',function(e){
            var color=e.data;
            document.getElementById('color').style.backgroundColor=color;
        },false);
    </script>
</body>
</html>
http://test.com/index.html
<!doctype html>
<html>
    <head>
        <style type="text/css">
            html,body{
                height:100%;
                margin:0px;
            }
        </style>
    </head>
    <body style="height:100%;">
        <p id="container" onclick="changeColor();" style="widht:100%; height:100%; background-color:rgb(204, 102, 0);">
            click to change color
        </p>
        <script type="text/javascript">
            var container=document.getElementById('container');
            window.addEventListener('message',function(e){
                if(e.source!=window.parent) return;
                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>
http://lslib.com/lslib.html

在例子中页面加载的时候主页面向iframe发送’getColor‘ 请求(参数没实际用处)

window.onload=function(){
            window.frames[0].postMessage('getcolor','http://lslib.com');
        }

iframe接收消息,并把当前颜色发送给主页面呢

window.addEventListener('message',function(e){
                if(e.source!=window.parent) return;
                var color=container.style.backgroundColor;
                window.parent.postMessage(color,'*');
            },false);

主页面接收消息,更改自己p颜色

window.addEventListener('message',function(e){
            var color=e.data;
            document.getElementById('color').style.backgroundColor=color;
        },false);

当点击iframe事触发其变色方法,把最新颜色发送给主页面

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事件的程序处理自身变色

window.addEventListener('message',function(e
  


 

您可能想查找下面的文章:

  • 详解HTML5 window.postMessage与跨域
  • html5 postMessage解决跨域、跨窗口消息传递方案
  • HTML5中使用postMessage实现两个网页间传递数据
  • html5跨域通讯之postMessage的用法总结
  • Html5 postMessage实现跨域消息传递
  • HTML5中使用postMessage实现Ajax跨域请求的方法
  • HTML5中的postMessage API基本使用教程
  • 详解html5 postMessage解决跨域通信的问题
  • Html5中postmessage实现子父窗口传值的代码
  • H5的window.postMessage与跨域

相关文章

  • 2018-12-03为什么说html5是移动互联网的趋势?
  • 2018-12-03html5和js绘制图片到canvas的方法
  • 2018-12-03H5表单验证失败该怎样提示
  • 2018-12-03H5读取文件并上传到服务器的方法
  • 2018-12-03html5规定元素是否可拖动的属性draggable
  • 2018-12-03正则表达式与HTML5新元素
  • 2017-08-06HTML5中新标签和常用标签详解
  • 2018-12-03HTML5 canvas基本绘图之图形变换
  • 2018-12-03浅谈H5的data-*中容易被忽略的一个小问题
  • 2018-12-03JavaScript之包装对象

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • Html5实现用户注册自动校验功能实例代码
    • HTML5-WebSocket实现对服务器CPU实时监控
    • 使用 HTML5 IndexedDB API
    • HTML5 canvas绘制的玫瑰花效果
    • 总结phonegap常用时间操作
    • 全面解析HTML5的文档结构和新增标签
    • HTML5无刷新改变当前url的代码
    • html5中设置或返回音视频是否在加载后即开始播放的属性autoplay
    • 使用canvas绘制超炫时钟
    • 基于 HTML5 Canvas 的 3D 渲染引擎界面以及吸附等效果的运用

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有