user11914177
user11914177

Reputation: 965

Javascript preventDefault() not working in Safari

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?

Upvotes: 0

Views: 1890

Answers (1)

Wais Kamal
Wais Kamal

Reputation: 6200

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.

Upvotes: 1

Related Questions