Read more

Beware: Many browsers define window.event

Dominik Schöler
October 01, 2015Software engineer at makandra GmbH

Some browsers define window.event, which will return a copy of the "current" event. However, this is not defined by the W3C. Most importantly, Firefox does not support it, neither do recent versions of IE.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

You should completely avoid accessing the global event and instead pass the event object around that the event handler receives. The easiest protection against accidentally accessing window.event is just never naming the event event, but e or evnt or so.

function brokenInFirefox() {
  event.preventDefault(); // Breaks in Firefox
}

function worksInAnyBrowser(event) {
  event.preventDefault(); // Works in most browsers
}

function workaround(e) {
  e.preventDefault(); // Protects against accidentally using window.event
}
Posted by Dominik Schöler to makandra dev (2015-10-01 13:32)