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

详解HTML5中的消息通信代码

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

本文主要包含HTML5,消息通信 等相关知识,匿名希望在学习及工作中可以帮助到您
HTML5支持跨文档消息通信(Cross-Document Messaging)。

既然使用到消息通信,那么必然有事件(event)产生。根据事件的产生和消费,我们能够找到发送者和接收者,也就是Sender和Listener。

其中Litener需要做如下的工作:

  1. 编写一个消息处理函数;

  2. 将消息处理函数注册:addEventListener('message', function, false);

其中Sender需要做以下工作:

  1. postMessage('this is a message', 'http://');

事件对象event中包含的成员包括:

  1. data:传递的数据;

  2. origin:origin,origin包括三要素:主机、协议、端口;

  3. source:来源对象;

好了,下面我们看一个例子,这个例子展示了在页面中嵌套页面并且向子页面发送消息:

父页面如下:

<!DOCTYPE html>
<html lang="en">

<!-- 
    crossDomain.html by Bill Weinman 
    <http://bw.org/contact/>
    created 2011-04-16

    Copyright (c) 2011 The BearHeart Group, LLC
    This file may be used for personal educational purposes as needed. 
    Use for other purposes is granted provided that this notice is
    retained and any changes made are clearly indicated as such. 
-->

<head>
    <title>
        HTML5 Messaging Template File (One)
    </title>
    <link rel="stylesheet" type="text/css" href="../CSS/main.css">
    <style>
        #frameTwo {
            float: left;
            width: 500px;
            height: 400px;
            margin: 0 5px;
            padding: 3px;
            border-top: 2px solid #3c6b92;
            border-left: 2px solid #3c6b92;
            border-bottom: 2px solid #ccc;
            border-right: 2px solid #ccc;
        }
        #content { height: 500px; }
    </style>
    <script type="text/javascript">
		// 域名
        var originTwo = 'http://two.3sn.net';
		// URL地址
        var URLTwo = 'http://two.3sn.net/H5Msg/ExerciseFiles/Chap01/crossDomainTwo.html';
        var windowTwo = null;

        function handleMessage(event) {
			// 判断源区域
            if (event.origin == originTwo) {
                if(!windowTwo) windowTwo = event.source;
                log('message from origin: ' + event.origin);
                log(event.data);
				// 发送消息
                windowTwo.postMessage('this is from windowOne!', originTwo);
                log('message sent back to windowTwo');
            } else {
                dispError('message from untrusted origin: ' + event.origin);
            }
        }


        function init() {
			// 添加消息处理函数
		    window.addEventListener("message", handleMessage, false);
            window.onerror = windowErrorHandler;
            log('this is windowOne');
            log('host: ' + location.host);
			
			// load two页面
            element('frameTwo').src = URLTwo;   // load the frame
        }

        // ##### Utilities #####

        // shortcut for getElementById
        function element(id) { return document.getElementById(id); }

        function clearDisp() {
            element('pageResults').innerHTML = '';
            element('message').innerHTML = '';
            element('message').className = '';
        }

        function dispMessage(message) {
            m = element('message');
            m.className = 'message';
            if(m.textContent.length > 0) {
                m.innerHTML += '<br />' + message;
            } else m.innerHTML = message;
        }

        function windowErrorHandler(message, filename, lineno) {
            dispError(message + ' (' + filename + ':' + lineno + ')' );
            return true;
        };

        function dispError(errorMessage) {
            element('pageResults').innerHTML += 
                errorMessage ? '<p class="error">' + errorMessage + '</p>\n' : '';
        }

        function log(m) {
            if(m.length < 1) return;
            logElement = element('log');
            if(logElement.textContent.length > 0) logElement.innerHTML += '<br />';
            logElement.innerHTML += nowTimeString() + ' ' + m;
        }

        function nowTimeString() {
            var d = new Date();
            return numToString(d.getUTCHours(), 2) + ':' + numToString(d.getUTCMinutes(), 2) + ':' +
                numToString(d.getUTCSeconds(), 2) + '.' + numToString(d.getUTCMilliseconds(), 3);
        }

        function numToString( num, len ) {
            var num = num + '';
            while(num.length < len) num = '0' + num;
            return num;
        }

        window.onload = init;

    </script>
</head>

<body>

<p id="content">

    <h1> 
        HTML5 Messaging Template File (One)
    </h1>

    <p id="message"></p>
    <p id="pageResults"></p>

    <iframe id="frameTwo">
        <p>Your browser doesn't support the iFrame feature</p>
    </iframe>

    <p id="log" style="font-family: monospace"></p>

</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">

<!-- 
    crossDomain.html by Bill Weinman 
    <http://bw.org/contact/>
    created 2011-04-16

    Copyright (c) 2011 The BearHeart Group, LLC
    This file may be used for personal educational purposes as needed. 
    Use for other purposes is granted provided that this notice is
    retained and any changes made are clearly indicated as such. 
-->

<head>
    <title>
        HTML5 Messaging Template File (Two)
    </title>
    <link rel="stylesheet" type="text/css" href="../CSS/main.css">
    <script type="text/javascript">
        var originOne = 'http://one.3sn.net';

        function handleMessage(event) {
            if (event.origin == originOne) {
                log('message from origin: ' + event.origin);
                log(event.data);
            } else {
                dispError('message from untrusted origin: ' + event.origin);
            }
        }

        // ##### Init #####

        function init() {
            window.onerror = windowErrorHandler;    // addEventListener doesn't provide the right error object in Firefox
            window.addEventListener("message", handleMessage, false);
            log('this is windowTwo');
            log('host: ' + location.host);
            var windowOne = parent;
            windowOne.postMessage('this is from windowTwo!', originOne);
            log('message sent to windowOne');
        }

        // ##### Utilities #####

        // shortcut for getElementById
        function element(id) { return document.getElementById(id); }

        function clearDisp() {
            element('pageResults').innerHTML = '';
            element('message').innerHTML = '';
            element('message').className = '';
        }

        function dispMessage(message) {
            m = element('message');
            m.className = 'message';
            if(m.textContent.length > 0) {
                m.innerHTML += '<br />' + m
  


 

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

  • HTML5知识点总结
  • HTML5的本地存储
  • HTML5本地存储之IndexedDB
  • Html5实现文件异步上传功能
  • Html5新标签datalist实现输入框与后台数据库数据的动态匹配
  • 详解HTML5 window.postMessage与跨域
  • HTML5拖放API实现拖放排序的实例代码
  • 解决html5中video标签无法播放mp4问题的办法
  • HTML5新特性 多线程(Worker SharedWorker)
  • Html5新增标签有哪些

相关文章

  • 2018-12-03html5 meter标签是什么意思?html5 meter标签的用法及属性介绍
  • 2018-12-03html5指南-2.如何操作document metadata_html5教程技巧
  • 2018-12-03Nodejs+express+html5 实现拖拽上传
  • 2018-12-03HTML5 对各个标签的定义与规定:aside
  • 2018-12-03详解HTML5中表单验证的8种方法介绍
  • 2017-08-06微信浏览器取消缓存的方法
  • 2017-08-06使用phonegap克隆和删除联系人的实现方法
  • 2017-08-06H5新属性audio音频和video视频的控制详解(推荐)
  • 2018-12-03百度搜索“2012世界末日”的效果是Javascript还是纯html5做出来的?
  • 2017-08-06IE9下html5初试小刀

文章分类

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

最近更新的内容

    • html5拍照功能实现代码(htm5上传文件)_html5教程技巧
    • HTML5的自定义属性data-*详细介绍和JS操作实例_html5教程技巧
    • HTML5 Canvas实现360度全景方法
    • phonegap常用事件总结(必看篇)
    • HTML5 Canvas绘制五星红旗_html5教程技巧
    • html5 ruby标签的定义及使用方法详解(内有实例介绍)
    • HTML5开发-在你的游戏应用中加入广告
    • HTML5 dialog是什么?怎么使用HTML5中的dialog来实现模拟弹窗?
    • 元素浮动的问题
    • html5游戏开发-零基础开发RPG游戏-开源讲座(二)-跑起来吧英雄

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

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