• 技术文章 >web前端 >H5教程

    html5新机制:postMessage实现安全跨域通信(代码)

    不言不言2018-08-17 14:36:12原创1282
    本篇文章给大家带来的内容是关于html5新机制:postMessage实现安全跨域通信(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    效果图

    161113223-5b762e6f55e2d_articlex.gif

    postmessage解析

    实现方式

    主界面 main.html

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

    <!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>

    相关推荐:

    postMessage处理iframe 跨域问题_html/css_WEB-ITnose

    H5中怎样使用postMessage实现两个网页间传递数据

    以上就是html5新机制:postMessage实现安全跨域通信(代码)的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:html
    上一篇:element-ui对话框可拖拽的功能如何实现?(附代码) 下一篇:html5 source标签怎么用?html5 source标签属性介绍
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• html5跨域通讯之postMessage的用法总结_html5教程技巧• Html5 postMessage实现跨域消息传递_html5教程技巧• HTML5中使用postMessage实现Ajax跨域请求的方法_html5教程技巧• HTML5 window.postMessage与跨域的实例教程• Html5中postmessage实现子父窗口传值的代码
    1/1

    PHP中文网