Beware: Many browsers define window.event

Updated . Posted . Visible to the public. Repeats.

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.

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
}
Dominik Schöler
Last edit
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2015-10-01 11:32)