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);

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

  • 相关阅读:
    地铁项目结对编程
    地铁项目初步计划及简单设计
    构建之法浅读感想
    集美大学1511,1512软件工程课程作业总结
    第二次作业小结
    第二次作业评分可能要 delay 一些
    第一次作业小结
    关于我
    面向对象设计与构造第四单元总结及期终总结
    面向对象设计与构造第三单元作业总结
  • 原文地址:https://www.cnblogs.com/CoreXin/p/postMessage.html
Copyright © 2011-2022 走看看