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

H5的window.postMessage与跨域

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

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

在前一篇文章中,讲到浏览器同源策略,即阻止不同域的页面间访问彼此的方法和属性,并对解决同源策略跨域问题提出的解决方案进行阐述:子域代理、JSONP、CORS。本篇将详细阐述HTML5 window.postMessage,借助postMessage API,文档间可以以安全可控的方式实现跨域通信,第三方JavaScript代码也可以与iframe内加载的外部文档进行通信。

HTML5 window.postMessage API

HTML5 window.postMessage是一个安全的、基于事件的消息API。

postMessage发送消息

在需要发送消息的源窗口调用postMessage方法即可发送消息。

源窗口

源窗口可以是全局的window对象,也可以是以下类型的窗口:

文档窗口中的iframe:

var iframe = document.getElementById('my-iframe');
    var win = iframe.documentWindow;

JavaScript打开的弹窗:

var win = window.open();

当前文档窗口的父窗口:

var win = window.parent;

打开当前文档的窗口:

var win = window.opener();

找到源window对象后,即可调用postMessage API向目标窗口发送消息:

``win.postMessage('Hello', 'ttp://jhssdemo.duapp.com/');"

postMessage函数接收两个参数:第一个为将要发送的消息,第二个为源窗口的源。

注:只有当目标窗口的源与postMessage函数中传入的源参数值匹配时,才能接收到消息。

接收postMessage消息

要想接收到之前源窗口通过postMessage发出的消息,只需要在目标窗口注册message事件并绑定事件监听函数,就可以在函数参数中获取消息。

window.onload = function() {
        var text = document.getElementById('txt');  
        function receiveMsg(e) {
            console.log("Got a message!");
            console.log("nMessage: " + e.data);
            console.log("nOrigin: " + e.origin);
            // console.log("Source: " + e.source);
            text.innerHTML = "Got a message!<br>" +
                "Message: " + e.data +
                "<br>Origin: " + e.origin;
        }
        if (window.addEventListener) {
            //为窗口注册message事件,并绑定监听函数
            window.addEventListener('message', receiveMsg, false);
        }else {
            window.attachEvent('message', receiveMsg);
        }
    };

message事件监听函数接收一个参数,Event对象实例,该对象有三个属性:

  1. data 发送的具体消息

  2. origin 发送消息源

  3. source 发送消息窗口的window对象引用

说明

  1. 可以将postMessage函数第二个参数设为*,在发送跨域消息时会跳过对发送消息的源的检查。

  2. postMessage只能发送字符串信息。

实例

源窗口

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Html5 postMessage</title>
        <style>
            #otherWin {
                width: 600px;
                height: 400px;
                background-color: #cccccc;
            }
        </style>
    </head>
    <body>
        <button id="btn">open</button>
        <button id="send">send</button>
         <!-- 通过 iframe 嵌入子页面(接收消息目标窗口) --> 
         <iframe src="http://jhssdemo.duapp.com/demo/h5_postmessage/win.html" 
                     id="otherWin"></iframe> 
         <br/><br/> 
         <input type="text" id="message"><input type="button" 
                 value="Send to child.com" id="sendMessage" /> 
        <script>
            window.onload = function() {
                var btn = document.getElementById('btn');
                var btn_send = document.getElementById('send');
                var sendBtn = document.getElementById('sendMessage');
                var win;
                btn.onclick = function() {
                    //通过window.open打开接收消息目标窗口
                    win = window.open('http://jhssdemo.duapp.com/demo/h5_postmessage/win.html', 'popUp');
                }
                btn_send.onclick = function() { 
                    // 通过 postMessage 向子窗口发送数据      
                    win.postMessage('Hello', 'http://jhssdemo.duapp.com/');
                }
                function sendIt(e){ 
                    // 通过 postMessage 向子窗口发送数据
                    document.getElementById("otherWin").contentWindow 
                    .postMessage( 
                        document.getElementById("message").value, 
                        "http://jhssdemo.duapp.com/"); 
                } 
                sendBtn.onclick = function(e) {
                    sendIt(e);
                };
            };
        </script>
    </body>
    </html>

目标窗口win.html

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Html5 postMessage</title>
        <style>
            #txt {
                width: 500px;
                height: 300px;
                background-color: #cccccc;
            }
        </style>
    </head>
    <body>
        <h1>The New Window</h1>
        <p id="txt"></p>
        <script>        
            window.onload = function() {
                var text = document.getElementById('txt');  
                //监听函数,接收一个参数--Event事件对象
                function receiveMsg(e) {
                    console.log("Got a message!");
                    console.log("nMessage: " + e.data);
                    console.log("nOrigin: " + e.origin);
                    text.innerHTML = "Got a message!<br>" +
                        "Message: " + e.data +
                        "<br>Origin: " + e.origin;
                }
                if (window.addEventListener) {
                    //为window注册message事件并绑定监听函数
                    window.addEventListener('message', receiveMsg, false);
                }else {
                    window.attachEvent('message', receiveMsg);
                }
            };
        </script>
    </body>
    </html>

相信看了本文案例你已经掌握了方法,更多精彩请关注微课江湖其它相关文章!

推荐阅读:

phonegap获取位置

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

  • H5的window.postMessage与跨域
  • HTML5 window.postMessage与跨域的实例教程
  • HTML5之window.postMessage API

相关文章

  • 2018-12-03h5canvas实现雪花飘落的特效代码
  • 2018-12-03js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别?
  • 2018-12-03HTML5拖放效果的实现代码
  • 2018-12-03如何配置H5的滚动条样式的代码
  • 2018-12-03程序设计HTML5 Canvas API _html5教程技巧
  • 2018-12-03html5 section标签是什么意思?html5 section标签的用法总结
  • 2018-12-03使用ASP.NET MVC构建HTML5离线web应用程序
  • 2017-08-06HTML5+Canvas+CSS3实现齐天大圣孙悟空腾云驾雾效果
  • 2018-12-03html5 canvas绘制矩形和圆形的实例代码
  • 2018-12-03利用HTML5的新特点实现图片文件异步上传_html5教程技巧

文章分类

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

最近更新的内容

    • HTML5之SVG 2D入门3—文本与图像及渲染文本介绍
    • html5游戏开发-零基础开发RPG游戏-开源讲座(四)-游戏脚本化&地图跳转
    • html5 常用标签汇总详情
    • 使用css如何制作时间ICON方法实践
    • HTML5 Canvas像素处理使用接口介绍
    • 检测浏览器对HTML5和CSS3支持度的方法
    • popstate如何使用?总结popstate实例用法
    • 我想做web前端,怎么学习 ?
    • 使用HTML5给按钮背景设计不同的动画的简单示例(图文)
    • html5的websockets全双工通信详解学习示例

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

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