62

How can I trigger some action with right click after disabling the browser context menu ?

I tried this . . .

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        $('.alert').fadeToggle();
        return false;
    });
});
Zac
  • 12,637
  • 21
  • 74
  • 122

8 Answers8

107

There is no built-in oncontextmenu event handler in jQuery, but you can do something like this:

$(document).ready(function(){ 
  document.oncontextmenu = function() {return false;};

  $(document).mousedown(function(e){ 
    if( e.button == 2 ) { 
      alert('Right mouse button!'); 
      return false; 
    } 
    return true; 
  }); 
});

Basically I cancel the oncontextmenu event of the DOM element to disable the browser context menu, and then I capture the mousedown event with jQuery, and there you can know in the event argument which button has been pressed.

You can try the above example here.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Isn't `$(document)[0]` the same as `document`? – configurator Mar 04 '11 at 00:04
  • @configurator, yes it's a typo. Thanks :) – Christian C. Salvadó Mar 04 '11 at 02:12
  • 35
    `"contextmenu"` is now supported! – nikow Feb 27 '12 at 09:33
  • This doesn't seem to work in firefox - only chrome. And very likely the numeric value of `e.button` is either browser or hardware specific – B T Aug 30 '13 at 16:55
  • 3
    It is better to use e.which (instead of e.button), which will give you the same value to all the browsers. – Marinos An Oct 18 '13 at 11:14
  • Chrome 39 on mac. e.which does not report 2, e.button == 2 works. Also 'contextmenu' does not work with addEventListener or with jquery's "on()". The MDN article also does not work: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contextmenu – Chris Jan 09 '15 at 23:01
  • This answer is invalid, contextmenu is triggered after right mousedown, not right click. – Sebastian Nowak Nov 27 '15 at 13:53
  • the line `document.oncontextmenu = function() {return false;};` doesn't make disappear the context menu in chrome v66... any working solution? – basZero May 02 '18 at 09:08
50

The function returns too early. I've added a comment to the code below:

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        return false;
        $('.alert').fadeToggle(); // this line never gets called
    });
});

Try swapping the return false; with the next line.

Bennett McElwee
  • 24,740
  • 6
  • 54
  • 63
15

Just use the event-handler. Something like this should work:

$('.js-my-element').bind('contextmenu', function(e) {
     e.preventDefault();
     alert('The eventhandler will make sure, that the contextmenu dosn't appear.');
});
outis
  • 75,655
  • 22
  • 151
  • 221
Simon
  • 151
  • 1
  • 2
2

I found this answer here and I'm using it like this.

Code from my Library:

$.fn.customContextMenu = function(callBack){
    $(this).each(function(){
        $(this).bind("contextmenu",function(e){
             e.preventDefault();
             callBack();
        });
    }); 
}

Code from my page's script:

$("#newmagazine").customContextMenu(function(){
    alert("some code");
});
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
1
document.oncontextmenu = function() {return false;}; //disable the browser context menu
$('selector-name')[0].oncontextmenu = function(){} //set jquery element context menu 
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • `document.oncontextmenu = function() {return false;};` doesn't work at all. Context menu still appears in browser (Chrome 66) – basZero May 02 '18 at 09:19
0

To disable right click context menu on all images of a page simply do this with following:

jQuery(document).ready(function(){
    // Disable context menu on images by right clicking
    for(i=0;i<document.images.length;i++) {
        document.images[i].onmousedown = protect;
    }
});

function protect (e) {
    //alert('Right mouse button not allowed!');
    this.oncontextmenu = function() {return false;};
}
InforMedic
  • 111
  • 1
  • 4
0

.contextmenu method :-

Try as follows

<div id="wrap">Right click</div>

<script>
$('#wrap').contextmenu(function() {
  alert("Right click");
});
</script>

.mousedown method:-

$('#wrap').mousedown(function(event) {

        if(event.which == 3){
            alert('Right Mouse button pressed.');
        }  
});
Arshid KV
  • 9,631
  • 3
  • 35
  • 36
0

Is contextmenu an event?

I would use onmousedown or onclick then grab the MouseEvent's button property to determine which button was pressed (0 = left, 1 = middle, 2 = right).

Alfred
  • 21,058
  • 61
  • 167
  • 249
BryanH
  • 5,826
  • 3
  • 34
  • 47