jQuery doesn't support postmessage event?

JqueryHtmlPostmessage

Jquery Problem Overview


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

$(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!

So, what's the problem?

Jquery Solutions


Solution 1 - Jquery

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

Solution 2 - Jquery

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstefanView Question on Stackoverflow
Solution 1 - JqueryFrédéric HamidiView Answer on Stackoverflow
Solution 2 - JqueryTiborView Answer on Stackoverflow