Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between window.onscroll and window.addEventListener

I can't find the difference between doing:

window.onscroll = () => console.log("scroll")

and this:

window.addEventListener('scroll', () => console.log("scroll"))

except for browser compatibility which both seems to be unsupported in most IE versions !

is it just a syntax difference? it seems that it is straightforward to remove the handler using removeEventListener, but I'm assuming window.onscroll = null has similar effect.

am I missing anything?

like image 705
Nour Avatar asked Mar 18 '20 18:03

Nour


1 Answers

The main differences is that there can only be one onscroll. Any additional onscrolls you add will override the first, whereas you can have as many .addEventListener('scroll', ()=>{}) as you want.

like image 164
Austin Ezell Avatar answered Nov 03 '22 01:11

Austin Ezell