window.postMessage在Mac中不起作用

时间:2017-04-01 18:33:47

标签: javascript jquery

我尝试使用带窗口的postMessage和框架内部。

这是代码:

窗口

function customHandler(callback){
    if (window.addEventListener) {  // all browsers except IE before version 9
        window.addEventListener ("message", callback, false);
    } else {
        if (window.attachEvent) {   // IE before version 9
            window.attachEvent("onmessage", callback);
        }
    }
}

customHandler(function(e) {
    console.log('WINDOW1: recv: ', e.data);

    if (e.data.status == true) {
        var data = {status: false, message: 'test'};
        console.log('WINDOW1: send: ', data);
        document.getElementById('regpopFrame').contentWindow.postMessage(data, "*");
    }
});


$(window).on('message', function(e) {
    console.log('WINDOW2: recv: ', e.originalEvent.data);

    if (e.originalEvent.data.status == true) {
        var data = {status: false, message: 'test'};
        console.log('WINDOW2: send: ', data);
        document.getElementById('regpopFrame').contentWindow.postMessage(data, "*");
    }
});

    window.onmessage = function(e) {
        console.log('WINDOW3: recv: ', e.data);

        if (e.data.status == true) {
            var data = {status: false, message: 'test'};
            console.log('WINDOW3: send: ', data);
            document.getElementById('regpopFrame').contentWindow.postMessage(data, "*");
        }
    };

的iFrame

function customHandler(callback){
    if (window.addEventListener) {  // all browsers except IE before version 9
        window.addEventListener ("message", callback, false);
    } else {
        if (window.attachEvent) {   // IE before version 9
            window.attachEvent("onmessage", callback);
        }
    }
}

customHandler(function(e) {
    console.log('FRAME1: recv: ', e.data);

    if (e.data.status == true) {

        var data = {status: true, message: 'test'};
        console.log('FRAME1: send: ', data);
        parent.postMessage(data, "*");
    }
});


$(window).on('message', function(e) {
    console.log('FRAME2: recv: ', e.originalEvent.data);

    if (e.originalEvent.data.status == true) {
        var data = {status: true, message: 'test'};
        console.log('FRAME2: send: ', data);
        parent.postMessage(data, "*");
    }
});

window.onmessage = function(e) {
    console.log('FRAME3: recv: ', e.data);

    if (e.data.status == true) {
        var data = {status: true, message: 'test'};
        console.log('FRAME3: send: ', data);
        parent.postMessage(data, "*");
    }
};


$(window).load(function(){
   parent.postMessage({status: true, message: 'test'}, '*');
});

此代码在我的浏览器中正常运行(Windows 10,Chrome) 但是在客户端浏览器(Mac,Safari或Chrome)或(Windows 10,Chrome)中没有。

我也试过了postMessage的起源,这也没有用。 这有什么问题?

0 个答案:

没有答案