0

refering to JavaScript window resize event

they mentioned they solution was to add a listener to the window with the "resize" event and said overriding window.resize is bad practise...

my question is why?

Community
  • 1
  • 1
Darth Joshua
  • 27
  • 2
  • 2
  • 5
  • 1
    Adding a listener is always preferred, so that several handlers can attach themselves to the same event. – Brad Mar 24 '14 at 15:09
  • 1
    "you're overriding the event and so you can't attach multiple actions to an event. The addEventListener/attachEvent combo is the best way to go to make your javascript play friendly with any other script that might be executing on the page." – RyanS Mar 24 '14 at 15:11
  • the comments above are right. And if you attach another script that overrides window.resize later, it will override your override. – Walter Brand Mar 24 '14 at 15:13

1 Answers1

1

The same applies to all events: using .onsomething = function() {...} is an assignment, and will overwrite any existing event handler, which can bugger up other pieces of code.

addEventListener, on the other hand, will add a new event handler without affecting existing ones.

One thing to watch out for is that I've seen numerous cases where the same event handler gets repeatedly added due to bad code, whereas using .onsomething = ... would avoid the problem.

Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540