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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)