admin管理员组

文章数量:1287281

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:

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:

Share Improve this question asked Jul 28, 2016 at 8:27 Lajos ArpadLajos Arpad 77k40 gold badges117 silver badges222 bronze badges 3
  • 2 Be aware that while preventing right click will stop the context menu appearing, I can still see the URL to the image and download it through the DOM viewer/dev tools. The old adage applies - if it's viewable online, anyone can take it. – Rory McCrossan Commented Jul 28, 2016 at 8:30
  • @RoryMcCrossan, that's ok, I just want the user not to see that menu, everything else is nice. – Lajos Arpad Commented Jul 28, 2016 at 8:32
  • Possible duplicate? stackoverflow./questions/4920221/… – Whothehellisthat Commented Jul 28, 2016 at 8:36
Add a ment  | 

1 Answer 1

Reset to default 13

You can use contextmenu:

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

本文标签: javascriptHow to prevent default right click on canvas containing imageStack Overflow