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

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

  • 相关阅读:
    git笔记
    微信扫码支付 php
    linux 下远程连接windows
    ubuntu15.10下搭建cordova+ionic开发环境
    Linux下磁盘分区挂载
    协议抓包分析软件
    MySQL分表的三种方法
    html5的audio实现高仿微信语音播放效果
    ThinkPHP页面跳转success与error方法
    jquery正则表达式验证(手机号、身份证号、中文名称)
  • 原文地址:https://www.cnblogs.com/CoreXin/p/postMessage.html
Copyright © 2011-2022 走看看