zoukankan      html  css  js  c++  java
  • html5 postMessage 消息传递问题

    <script type="text/javascript">
            $.fn.extend({
                addEvent: function (type, handle, bool) {
                    var el, thisLen = this.length;
                    bool ? bool = bool : bool = false;
                    if (thisLen == 1) {
                        el = this[0];        //jquery对象转成 js对象
                        el.addEventListener ? el.addEventListener(type, handle, bool) :
                                el.attachEvent('on' + type, handle);
                    } else {
                        for (var i = 0; i < thisLen; i++) {
                            el = this[i];
                            el.addEventListener ? el.addEventListener(type, handle, bool) :
                                    el.attachEvent('on' + type, handle);
                        }
                    }
                }
            });
        </script>

    兼容IE 与Firefox浏览器

            var messageInfo = new Object;
            messageInfo.received = '/HumanResource/Employee/Index';
            messageInfo.infoType = 'technical lead';   
            messageInfo.data = 'technical lead';        
            parent.postMessage(JSON.stringify(messageInfo), window.location);

    子窗口向父窗口传递消息

        $(window).addEvent('message', function (ev) {        
            var messageInfoJson = JSON.parse(ev.data);
            var received = messageInfoJson.received;
            if (received != '/Home/Index') {
                console.log('转发消息');
                $("iframe").each(function (i) {
                    console.log(ev.data);
                    console.log(messageInfoJson);
                    window.frames[i].postMessage(ev.data, window.location);
                });
            }
            else {
                alert('哦,Home index 我被点啦。。。' + messageInfo.data);
    
                console.log('哦,Home index 我被点啦。。。');
                console.log(ev);
            }
        }, false);

    接受消息,如不需要自己处理,则转发消息给其他窗口

  • 相关阅读:
    个人作业——软件评测
    软件工程实践2019第五次作业
    18年今日头条笔试第一题题解:球迷(fans)
    游戏2.1版本
    游戏2.0版本 代码
    游戏2.0版本
    改进版游戏代码
    改进版游戏
    2017.1.13之审判日
    找朋友 的内存超限代码
  • 原文地址:https://www.cnblogs.com/CoreXin/p/postMessage.html
Copyright © 2011-2022 走看看