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

2

17

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

Obduliaobdurate answered 16/12, 2016 at 9:44 Comment(1)
I'm running Chrome 88 and they definitely haven't removed onmousedown and onmouseup. That would be a really dumb move as it would break a ton of websites. Best guess is it was a bug in this version of Chrome.Circumscription
O
23

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

Obduliaobdurate answered 20/12, 2016 at 9:25 Comment(6)
By adding both mousedown, pointerdown and touchstart, wouldn't the function be called 2-3 times? For example, in that function we increment a number, then in this case it will be incremented more than we wanted. If so, what to do in order to prevent the other calls? ThanksHannahannah
I think browsers cover this, but to be sure, you can use the updated code in the example!Obduliaobdurate
Well, I've tested it in HTML (not in JavaScript), and had: <Button onmousedown='someFunc()' ontouchstart='someFunc() onpointerdown='someFunc()>Click<Button/> And the function was called 2 timers on Chrome desktop and 3 time on mobile phone (probably the touch event is the one extra on phone). So I don't think that registering the event from JavaScript would change anything. Thanks for the updated code.Hannahannah
Why remove the event listeners?Loon
What is isEventSupported()? It's not a DOM method, and it's not defined here.Dreibund
Nope, nothing got ditched - the mouse events are still firing in all major browsers (including Chrome) in 2022.Anisaanise
S
2

To add to @WouterCoebergh solution. You must remember to add a fallback for the old event. This can be done by the following, taken from the same[1] source.

var mousedownEvent = function(ev) {
  o.clickDown(ev);
}

var elm = document.getElementById("floorplan-backdrop-rect");

if (window.PointerEvent) {
  elm.addEventListener('pointerdown', mousedownEvent);
} else {
  elm.addEventListener('mousedown', mousedownEvent);
}

[1]: Google Developers Blog

Samovar answered 29/3, 2017 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.