I was wondering what is the difference between those two ways of listening a message.
Here is my assumptions:
window.onmesage = onMessage
This assign a function to the onmessage
attribute of the window
object. It will be executed every time I receive a message on my page. It allows to have only 1 listener for messages.
vs.
window.addEventListener("message", onMessage);
This adds an event listener to the message
event. It will be executed every time I receive a message on my page. It allows to have multiple listener for messages.
Differences
If I call many times the first one, I will always have 1 function that is executed when receiving a message.
If I call X times the second one, I will have many listeners and when receiving a message, my onMessage
function will be executed X times
Question
Are my assumptions correct?