jQuery 不支持 postmessage 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9904490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:26:26  来源:igfitidea点击:

jQuery doesn't support postmessage event?

jqueryhtmlpostmessage

提问by stefan

When I use jQuery event listener to handle message event, like below:

当我使用 jQuery 事件侦听器处理消息事件时,如下所示:

$(window).on('message', function(e) {
    var data = e.data; // data = undefined
});

data is undefined! I'm sure that I have passed data to current window. Because if I use "addEventListener", everything goes well!

数据未定义!我确定我已经将数据传递到当前窗口。因为如果我使用“addEventListener”,一切都会顺利!

So, what's the problem?

所以有什么问题?

回答by Frédéric Hamidi

jQuery might be preprocessing the event's dataproperty, and this operation may not properly support the messageevent (yet).

jQuery 可能正在预处理事件的data属性,并且此操作可能无法正确支持该message事件(目前)。

Try using the originalEventproperty to fetch your data:

尝试使用该originalEvent属性来获取您的数据:

$(window).on("message", function(e) {
    var data = e.originalEvent.data;  // Should work.
});

回答by Tibor

Some browsers use the "onmessage" event. I suggest a little improvement to the previous answer for increased compatibility:

一些浏览器使用“onmessage”事件。我建议对先前的答案进行一些改进以提高兼容性:

$(window).on("message onmessage", function(e) {
    var data = e.originalEvent.data;
});