HTML5跨域信息交互技术之postMessage代码实例详解


本文摘自PHP中文网,作者黄舟,侵删。

我们知道传统的HTML 规范中对于跨域的请求有这严格的限制,如果没有这个限制,将会发生很可怕的事情,设想一个场景当你在公司上班打开公司内部的管理信息系统,同时你打开了另一个外部网页页面, 那个外部网页中的动态脚本比如JS 脚本可以嗅探到你公司内部管理信息系统的内容,假如你公司的信息系统是一些敏感的信息时,其实你在不知不觉中已经泄漏了公司的信息,由此可能将会造成给公司很大的损失,所以浏览器是阻止这些跨域访问。

但是现实生活中有一些合理的跨域名站点间的交互,读者可能知道 传统HTML 规范中关于跨域的解决方法,
比如 iframe方式、jsonp方式等,今天我要说的是HTML5 中关于跨域数据交互方面的知识。

HTML5中引入了 一个新的API 称为 postMessage ,其实postMessage不管是否有跨域操作, 都建议使用postMessage 来传递消息。

废话不多说,看一个Demo 先。
我们配置两个域名 http://www.yuetong.com/
http://my.bbs.com/
在 http://www.yuetong.com/ 域下新建 comm_main.html, 地址 为http://www.yuetong.com/comm_main.html
文件内容如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

<!DOCTYPE html>

<html>

<head>   

    <meta charset="utf-8"/>

    <title>跨站通信 当前域 http://www.yuetong.com</title>

    <script type="text/javascript">

function sendMsg(){

    if(typeof window.postMessage == undefined){

        //

        alert("对不起 您的浏览器不支持 postMessage 特性");

        return false;

    }

    var msg = document.getElementById("message").value;

    document.getElementsByTagName("iframe")[0].contentWindow.postMessage(msg,"http://my.bbs.com");

}

var originWhiteList = ["http://my.bbs.com"];

function checkWhiteList(origin){

    for(var i=0; i< originWhiteList.length; i++){

        if(origin == originWhiteList[i]){

            return true;

        }

    }

    return false;

}

/** 接受消息 */

function messageHandler(e){

    if(checkWhiteList(e.origin)){

        processMessage(e.data);

    }else{

        // ignore message

    }

}

function processMessage(d){

    alert(d);

}

window.addEventListener("message", messageHandler, true);

    </script>

</head>

<body>

<h1>您好,我这里是http://www.yuetong.com/</h1>

<input type="text" name="message" id="message" value=""/>

<input type="button" name="handler" value="发生消息" onclick="sendMsg()"/>

<br/>

<iframe name="msg" src="http://my.bbs.com/comm_client.html" width="400" height="400"/>

<p></p>

</body>

</html>

在 http://my.bbs.com/ 域下新建 comm_client.html, 地址 为http://my.bbs.com/comm_main.html,文件内容如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<!DOCTYPE html>

<html>

<head>   

    <meta charset="utf-8"/>

    <title>跨站通信 当前域 http://my.bbs.com</title>

<script type="text/javascript">

var originWhiteList = ["http://www.yuetong.com"];

function checkWhiteList(origin){

    for(var i=0; i< originWhiteList.length; i++){

        if(origin == originWhiteList[i]){

            return true;

        }

    }

    return false;

}

function messageHandler(e){

    if(checkWhiteList(e.origin)){

        processMessage(e.data);

    }else{

        // ignore message

    }

}

function processMessage(d){

    var ta = document.getElementsByTagName("textarea")[0].value;

    ta += d + "\n";

    document.getElementsByTagName("textarea")[0].value = ta;

}

function sendMsg(){

    var msg = document.getElementById("message").value;

    window.top.postMessage(msg,"http://www.yuetong.com");

}

window.addEventListener("message",messageHandler,true);

</script>

</head>

<body>

<h1>您好,我这里是 http://my.bbs.com/</h1>

<input type="text" name="message" id="message" value=""/>

<input type="button" name="handler" value="发生消息" onclick="sendMsg()"/>

<textarea name="msg" rows="15" cols="40"></textarea>

</body>

</html>

配图1,当再输入框中输入"你好,朋友" 可以看到iframe窗口中 收到消息。

\

配图1

我们在iframe 窗口中输入“我很好" ,主窗口收到消息并弹出 提示框

\

其中最为重要 为增加 message 消息的监听和处理, 以及信任站点的配置。

以上就是HTML5跨域信息交互技术之postMessage代码实例详解的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

讲讲HTML5中被废弃的标签

关于HTML5中自定义属性的介绍

如何使用HTML5的page visibility api来实现获取焦点js事件

HTML5怎么实现图片转圈的动画效果

服务端主动发送数据回客户端在h5里的实现步奏

HTML5中转义实体字符,元数据, 跳转以及全局属性的图文详解

如何使用HTML5 canvas绘制线条

h5本地存储实例详解

HTML5 navigator.geolocation基于浏览器获取地理位置代码案例

HTML5中重新加载音频/视频元素的方法load()

更多相关阅读请进入《HTML5》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...