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

前端 未结 1 742
慢半拍i
慢半拍i 2020-11-29 13:05

While working on an answer for another question, a strange bug came up related to the event object being available in an anonymous function without being passed

相关标签:
1条回答
  • 2020-11-29 13:58

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

    In in FF and other browsers the event object was passed as an argument, since in FF there is no global property called event, you are getting the error message.

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

    But since you are using jQuery, jQuery normalizes 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);
        });
    });
    
    0 讨论(0)
提交回复
热议问题