Lajos Arpad Lajos Arpad - 4 months ago 14
Javascript Question

How to prevent default right click on canvas containing image

I have a canvas and I display an image inside it. I have attached a jquery event to it, like this:

$("#mycanvas").mousedown(function(e) {
//Do something
e.preventDefault();
e.stopPropagation();
});


I would expect this code to do my operations and to prevent default browser behavior. The former is fulfilled, however, the latter, namely, default behavior prevention does not happen. The event runs though. I wonder how could I prevent showing that menu you can see on the image upon right-click:

enter image description here

Answer

You can use contextmenu:

$("#mycanvas").contextmenu(function(e) {
    //Do something
    e.preventDefault();
    e.stopPropagation();
});
Comments