详解html5 postMessage解决跨域通信的问题


本文摘自PHP中文网,作者青灯夜游,侵删。

这篇文章主要介绍了详解html5 postMessage解决跨域通信的问题的相关资料,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

本文介绍了详解html5 postMessage解决跨域通信的问题,分享给大家,具体如下:

效果图:

postmessage解析HTML5提供了新型机制PostMessage实现安全的跨源通信.

语法:

1

otherWindow.postMessage(message, targetOrigin, [transfer]);

otherWindow:其他窗口的一个引用, 比如IFRAME的contentWindow属性, 执行,window.open返回的窗口对象。

message:将要发送到其他窗口的数据。

targetOrigin:通过窗口的origin属性来指定哪些窗口能接收到消息事件, 其值可以是字符”*”(表示无限制)或者一个URL transfer:是一串和message同时传递的Transferable对象。 这些对象的所有权将被转移给消息的接收方, 而发送一放将不再保有所有权。

element.addEventListener(event,fn,useCaption ); 三个参数 event 事件 比如click mouseenter mouseleave 回调函数 useCaption用于描述是冒泡还是捕获。默认值是false,即冒泡传递。 当值为true,就是捕获传递。实现方式

主界面 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

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>跨域数据访问</title>

  <script type="text/javascript">

         window.addEventListener('message',function(e){

           console.log("e--->",e);

           const data = e.data;

           document.getElementById('main1').style.backgroundColor=e.data;

         },false)

 

  </script>

</head>

<body>

  <p id="main1" style="width:200px;height:200px;margin:100px;border:solid 1px #000;">

     我是主界面,等待接收iframe的传递

  </p>

  <p style="margin:100px;">

     iframe

     <iframe src="http://localhost:3000/iframe.html" width="800px" height="300px" ></iframe>

  </p>

</body>

</html>

iframe界面

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

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>Document</title>

    <style type="text/css">

           html,body{

               height:100%;

               margin:0px;

           }

    </style>

</head>

  <body style="height:100%;">

        <p id="frame" style="height:200px; width:200px;background-color:rgb(204, 204, 0)" onclick="changeColor()">

           点击改变颜色

        </p>

        <script type="text/javascript">

             function changeColor(){

               var frame = document.getElementById('frame');

               var color=frame.style.backgroundColor;

               if(color=='rgb(204, 102, 0)'){

                   color='rgb(204, 204, 0)';

               }else{

                   color='rgb(204,102,0)';

               }

                console.log("frame===>",frame);

                console.log("color",color);

               frame.style.backgroundColor=color;

               window.parent.postMessage(color,'*');

             }

        </script>

  </body>

</html>

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问Html5视频教程!

相关推荐:

php公益培训视频教程

HTML5图文教程

HTML5在线手册

以上就是详解html5 postMessage解决跨域通信的问题的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

使用php和HTML5 formdata实现无刷新文件上传

如何使用css设计出一个表单页面(附示例)

使用HTML5实现树叶飘落的效果

HTML5 是什么意思

使用adobe HTML5 extension的开发详解

详细介绍7款炫酷的HTML5 canvas动画特效

HTML5在工作中主要干什么

HTML5画一个简单呢好看的电路图

图文详解如何用canvas画实心圆和空心圆

详解h5的自定义属性data-*

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




打赏

取消

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

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

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

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

评论

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