Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 917
stefan Avatar asked Mar 28 '12 09:03

stefan


People also ask

What is postMessage in Javascript?

The window.postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.

Why use postMessage?

postMessage() is used by the application to allow cross-origin communication between different window objects, e.g. between a page and a pop-up that it spawned or between a page and an iframe embedded within it. This method provides a way to circumvent the Same Origin Policy restrictions securely.


1 Answers

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. }); 
like image 73
Frédéric Hamidi Avatar answered Oct 07 '22 00:10

Frédéric Hamidi