Bind event to right click

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

I tried this.,.

$(document).ready(function(){ $(document).bind("contextmenu",function(e){ $('.alert').fadeToggle(); return false; }); }); 
+58
jquery css
Apr 01 '09 at 17:47
source share
8 answers

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 using jQuery, and there you can find out in the event argument which button was pressed.

You can try the above example here .

+93
Apr 01 '09 at 18:07
source share

Function returns too soon. I 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 replacing return false; to the next line.

+47
May 05 '10 at 3:24
source share

Just use an 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.'); }); 
+15
Aug 27 '11 at 17:52
source share

I found this answer here, and I use 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 script page:

 $("#newmagazine").customContextMenu(function(){ alert("some code"); }); 
+2
Jan 02 '14 at 2:08
source share
 document.oncontextmenu = function() {return false;}; //disable the browser context menu $('selector-name')[0].oncontextmenu = function(){} //set jquery element context menu 
+1
Nov 25 '14 at 11:38
source share

Is contextmenu event?

I would use onmousedown or onclick , then capture the MouseEvent property of the button to determine which button was clicked (0 = left, 1 = middle, 2 = right).

0
Apr 01 '09 at 18:09
source share

To disable the context menu of the context menu on all images of the page, simply do the 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;}; } 
0
Dec 01 '14 at 11:37
source share

.contextmenu : -

Try the following

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

.mousedown : -

 $('#wrap').mousedown(function(event) { if(event.which == 3){ alert('Right Mouse button pressed.'); } }); 
0
Mar 03 '16 at 7:04 on
source share



All Articles