Print Page

Wednesday, November 6, 2013

Event handling difference in Chrome and Firefox

Our QA engineer Oleg found a bug in a piece of Javascript I wrote last week that worked for me in Chrome but failed for him in Firefox. I found the answer quickly on the internet, but I'm writing it up here again to help sink it into my brain.

Event handling is different in Chrome's V8 Javascript engine and Firefox's Spidermonkey Javascript engine. 

In Chrome you can say: 

$("table#organisms_table").on("click","button.organism_copy", () -> 
    clicked_row = $(event.target).closest("tr") 
    .... do stuff with the row ...


and have event work as an implied variable in the anonymous function that handles the click event. 

In Firefox you must explicitly pass in the event to the function. So for Firefox the code must be: 

  $("table#organisms_table").on("click","button.organism_copy", (event) -> 
    clicked_row = $(event.target).closest("tr")

        .... do stuff with the row ...

With the implied version, the handler failed in Firefox with event being undefined and since it happened to be in a form it processed unintentionally like a submit button.

No comments:

Post a Comment