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
jQuery doesn't support postmessage event?
提问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 data
property, and this operation may not properly support the message
event (yet).
jQuery 可能正在预处理事件的data
属性,并且此操作可能无法正确支持该message
事件(目前)。
Try using the originalEvent
property 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;
});