Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
It's not the same,
addEventListener adds one listener,
while onmessage replaces all with
you function -
Still, I don't know if that was his specific issue, but, in my limited frontend knowledge, shouldn't his callback be invoked no matter how he registered it?
-
@ElectroArchiver So if there are 5 different components using “window.onmessage”, there will actually be only one registered listener?
-
@Cyanide Exactly, you can try this in a new tab with devtools console opened:
window.onclick = () => console.log('test1')
window.onclick = () => console.log('test2')
You will only see 'test2' in the log when you click -
@ElectroArchiver
Yes, but he specifically mentions that he fixed it by switching to .onmessage, which suggests he only has one listener whatsoever, which, again, *should*have been called with .addEventListener too. -
@CoreFusionX Sure, I was answering his questions on how JS works, nothing more.
-
@ElectroArchiver
Yeah, all good. Yet I still want to know how it even happened in the first place. 😂 -
@CoreFusionX Well, he might have stopped event propagation in another listener..
Related Rants
I had been banging my head on an issue for 2 days. The solution?
Replaced
“window.addEventListener(“message”, callback)”
with
“window.onmessage = callback”
What. The. Actual. Fuck??!!
Aren’t they supposed to be the same thing?? It’s written on MDN’s article that the latter is an alternative way of doing the former.
What the fuck then? I am actually really pissed right now. Ragefully pissed.
rant
event
mdn
listener
web