Gotcha: firing Mouse Events Chrome vs Firefox

15Jun11

AuthorAnton Savchenko (Software Engineer)

Level: Intermediate

Description: knows their way around Chrome and Firefox extension development

Javascript provides an initMouseEvent method for initializing and firing mouse click events. Although the syntax is the same across Chrome and Firefox the behavior is slightly different. The documentation states that all parameters are mandatory and Firefox spews out error messages if any are omitted, however Chrome will silently continue passing in defaults for missing parameters.

Developers should be cautions of this and provide all parameters to prevent unexpected behaviors and ensure cross browser functionality.

event.initMouseEvent(type, canBubble, cancelable, view,
                     detail, screenX, screenY, clientX, clientY,
                     ctrlKey, altKey, shiftKey, metaKey,
                     button, relatedTarget);

The full documentation for initMouseEvent is available on the Mozilla Developer Network.

Creating mouse event example

var click = document.createEvent('MouseEvents');
click.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, 
false, false, 0, null);
document.getElementById('button').dispatchEvent(click);

As usual, IE keeps us in business by being a completely different story…

Advertisements


%d bloggers like this: