Why is "event" available globally in Chrome but not FF?

While working on the answer to another question, there was a strange error related to the fact that an object event

is available in an anonymous function without passing. In Chrome, below works fine, but FF throws an error.

$(document).ready(function() {
  $("#uspsSideboxTrackingClose").click(function() {
    event.preventDefault();
    console.log(event);
  });
});

      

Chromium:

enter image description here

Fire Fox:

ReferenceError: event undefined


It is already known that

$("#uspsSideboxTrackingClose").click(function(event) { .. }

      

works in both browsers. Here is the violation code. Is this a bug with Chrome or FF, or the intended behavior of both browsers? Which browser is right?

+6


source to share


1 answer


In IE, the event object was a global object (not passed to a handler function) but is available as a global object. You can also access it as a property of the window object likewindow.event

In FF and other browsers, the event object was passed as an argument, since there is no global property in FF event

, you get an error.

In chrome, they added support for both of these functions, so you get the event object as a global reference and as an argument.



But since you are using jQuery, jQuery will normalize these 2 behaviors and will always pass the event object as an argument to the event handler.

$(document).ready(function () {
    $("#uspsSideboxTrackingClose").click(function (event) {
        event.preventDefault();
        console.log(event);
    });
});

      

+11


source







All Articles