stefan
stefan

Reputation: 2477

jQuery doesn't support postmessage event?

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?

Upvotes: 67

Views: 30640

Answers (2)

Tibor
Tibor

Reputation: 229

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

Upvotes: 15

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263017

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

Upvotes: 118

Related Questions