Search code examples
javascriptgoogle-chromemouseevent

Chrome mousedown and mouseup events no longer working, other browsers are fine


As of today (or yesterday, didn't notice it then), mousedown and mouseup events are no longer working. I am on Chrome Version 55.0.2883.95 (64-bit). Safari and FireFox are working fine (I am on a Mac computer).

Here is the code:

document.getElementById("floorplan-backdrop-rect").addEventListener('mousedown', function(ev) {
    o.clickDown(ev);
}, false);

document.getElementById("floorplan-backdrop-rect").addEventListener('mouseup', function(ev) {
    o.clickUp(ev);
}, false);

Were there any API changes regarding mouse-events that we missed? Chrome does not throw any warning when registering the events either. Touchdown and touch up event seem to fail too (in emulated iPad mode in developer tools)

EDIT: Right after changing tab, or when resizing the window, the events seem to come through for a short while. Then they stop again..

Regards


Solution

  • EDIT (see old answer below):

    Chrome ditched mouse events in favour of pointer events from version 55 and up.

    Why (W3C):

    Today, most [HTML5] content is used with and/or designed for mouse input. Those that handle input in a custom manner typically code to [DOM-LEVEL-3-EVENTS] Mouse Events. Newer computing devices today, however, incorporate other forms of input, including touchscreens, pen input, etc. Event types have been proposed for handling each of these forms of input individually. However, that approach often incurs unnecessary duplication of logic and event handling overhead when adding support for a new input type. This often creates a compatibility problem when content is written with only one device type in mind. Additionally, for compatibility with existing mouse-based content, most user agents fire Mouse Events for all input types. This makes it ambiguous whether a Mouse Event represents an actual mouse device or is being produced from another input type for compatibility, which makes it hard to code to both device types simultaneously.

    Usable code:

    To add different event listeners for the "same" event, use the following code:

    // Put these in seperate function instead of anonymous ones
    // since you will need them later to deregister the event
    function onPointerDown(event){ /** Do stuff here **/ }
    function onPointerHover(event){ /** Do stuff here **/ }
    function onPointerMove(event){ /** Do stuff here **/ }
    function onPointerUp(event){ /** Do stuff here **/ }
    
    // Add event listeners
    if (isEventSupported("onpointerdown")) {
        domElement.addEventListener("pointerdown", onPointerDown, false);
        domElement.addEventListener("pointermove", onPointerHover, false);
        domElement.addEventListener("pointermove", onPointerMove, false);
        domElement.addEventListener("pointerup", onPointerUp, false);
    } else if (isEventSupported("ontouchstart")) {
        domElement.addEventListener("touchstart", onPointerDown, false);
        domElement.addEventListener("touchmove", onPointerHover, false);
        domElement.addEventListener("touchmove", onPointerMove, false);
        domElement.addEventListener("touchend", onPointerUp, false);
    } else if (isEventSupported("onmousedown")) {
        domElement.addEventListener("mousedown", onPointerDown, false);
        domElement.addEventListener("mousemove", onPointerHover, false);
        domElement.addEventListener("mousemove", onPointerMove, false);
        domElement.addEventListener("mouseup", onPointerUp, false);
    }
    
    // Remove event listeners
    if (isEventSupported("onpointerdown")) {
        domElement.removeEventListener("pointerdown", onPointerDown, false);
        domElement.removeEventListener("pointermove", onPointerHover, false);
        domElement.removeEventListener("pointermove", onPointerMove, false);
        domElement.removeEventListener("pointerup", onPointerUp, false);
    } else if (isEventSupported("ontouchstart")) {
        domElement.removeEventListener("touchstart", onPointerDown, false);
        domElement.removeEventListener("touchmove", onPointerHover, false);
        domElement.removeEventListener("touchmove", onPointerMove, false);
        domElement.removeEventListener("touchend", onPointerUp, false);
    } else if (isEventSupported("onmousedown")) {
        domElement.removeEventListener("mousedown", onPointerDown, false);
        domElement.removeEventListener("mousemove", onPointerHover, false);
        domElement.removeEventListener("mousemove", onPointerMove, false);
        domElement.removeEventListener("mouseup", onPointerUp, false);
    }
    

    References:


    Old answer:


    Looks like chrome ditched mouse events in favour of pointer events from version 55 and up.

    Changing the original code to the following fixed our problem for chrome:

    note: using this is not recommended since we can not deregister the listeners like this, see new example below.

    document.getElementById("some-id").addEventListener('pointerdown', function(ev) {
        o.clickDown(ev);
    }, false);
    
    document.getElementById("some-id").addEventListener('pointerup', function(ev) {
        o.clickUp(ev);
    }, false);
    

    Note that if you have additional checks on the event type, like we did, the type also changed from 'mouseup' to 'pointerup' and from 'mousedown' to 'pointerdown'

    You can read up on the update article here:

    https://developers.google.com/web/updates/2016/10/pointer-events