0

I am working on an application using javascript and I want to get mouse events. To stop the options that appear when right clicking I use the preventDefault() function and it works in Firefox and Chrome but it doesn't work in Safari. This is my code:

document.querySelector("#GL-Surface").addEventListener("mousedown", function(e) {
    e.preventDefault();

    /* Handle mouse events */
});

From an other question I got that you should return false; but this still doesn't work. preventDefault() however works in Safari when it is used in keyboard inputs. So how can I prevent the default actions for mouse events in Safari?

user11914177
  • 885
  • 11
  • 33
  • 1
    Hey, can you try `e.stopPropagation()` or `e.stopImmediatePropagation()`. :) – halilcakar Jun 07 '20 at 12:05
  • @HalilÇakar Both commands don't work – user11914177 Jun 07 '20 at 12:26
  • 1
    Ohh, yea aight probably the result from @Wais Kamal is correct for right click events. also here is another one from stackoverflow https://stackoverflow.com/questions/26032971/how-do-you-disable-click-events-from-the-contextmenu-event-when-using-ctrlclick – halilcakar Jun 07 '20 at 12:29

1 Answers1

1

To target right click events, use contextmenu rather than mousedown.

document.querySelector("#GL-Surface").addEventListener("contextmenu", function(e) {
    e.preventDefault();
});

Note that the options that appear on right click do appear only when the right click button is released, so I don't think mousedown is at all suitable here.

Wais Kamal
  • 5,858
  • 2
  • 17
  • 36