jQuery doesn't support postmessage event?

前端 未结 2 351
悲哀的现实
悲哀的现实 2021-02-01 12:30

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

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


        
相关标签:
2条回答
  • 2021-02-01 12:30

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

    $(window).on("message onmessage", function(e) {
        var data = e.originalEvent.data;
    });
    
    0 讨论(0)
  • 2021-02-01 12:31

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

    Try using the originalEvent property to fetch your data:

    $(window).on("message", function(e) {
        var data = e.originalEvent.data;  // Should work.
    });
    
    0 讨论(0)
提交回复
热议问题