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?
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.
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.
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. });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With