最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable mouse right click menu when shift is being pressed in firefox? - Stack Overflow

programmeradmin4浏览0评论

I'm trying to disable the mouse right click option. So i used contextmenu bind function to prevent it. This works fine but when shift is pressed along with the mosue right click the contextmenu bind function is not triggering but it shows the contextmenu. Means am not getting the alert but it shows the menu.

Here is the code i tried.

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
   alert('Context Menu event has fired!');
   return false;
}); 
});

In order to capture the shift button press and mouse right click am doing the below code but this doesn't help. May be i am doing something wrong.

$(document).ready(function(){

    $(document).bind("contextmenu",function(e){
   alert('Context Menu event has fired!');
   return false;
}); 

    var shift = false;
jQuery(document).on("keydown", function(event) {
            //check for shift key is pressed
        if (event.which === 16) { 
        shift = true;
                        }
});
jQuery(document).mousedown(function(e) {
     // e.which === 3 is for mouse right click
   if (e.which === 3 && shift === true) {
console.log("both action are triggered");
       return false; // how to stop the contextmenu action here
                           }
});
});

I tried giving the e.preventDefault instead of return false. I think the context menu event itself is not triggering in firefox when shift is clicked.

How to disable the mouse right click in this situation for firefox? Any help or clue will be much helpful

JSFIDDLE NOTE This is not happening in chrome. This is happening in firefox only. Is this a bug?

I'm trying to disable the mouse right click option. So i used contextmenu bind function to prevent it. This works fine but when shift is pressed along with the mosue right click the contextmenu bind function is not triggering but it shows the contextmenu. Means am not getting the alert but it shows the menu.

Here is the code i tried.

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
   alert('Context Menu event has fired!');
   return false;
}); 
});

In order to capture the shift button press and mouse right click am doing the below code but this doesn't help. May be i am doing something wrong.

$(document).ready(function(){

    $(document).bind("contextmenu",function(e){
   alert('Context Menu event has fired!');
   return false;
}); 

    var shift = false;
jQuery(document).on("keydown", function(event) {
            //check for shift key is pressed
        if (event.which === 16) { 
        shift = true;
                        }
});
jQuery(document).mousedown(function(e) {
     // e.which === 3 is for mouse right click
   if (e.which === 3 && shift === true) {
console.log("both action are triggered");
       return false; // how to stop the contextmenu action here
                           }
});
});

I tried giving the e.preventDefault instead of return false. I think the context menu event itself is not triggering in firefox when shift is clicked.

How to disable the mouse right click in this situation for firefox? Any help or clue will be much helpful

JSFIDDLE NOTE This is not happening in chrome. This is happening in firefox only. Is this a bug?

Share Improve this question edited Apr 1, 2014 at 6:02 sun asked Mar 31, 2014 at 11:42 sunsun 1,67811 gold badges29 silver badges48 bronze badges 1
  • jsfiddle/guvenaltuntas/9RYsb also tried and failed – Güven Altuntaş Commented Apr 1, 2014 at 9:51
Add a ment  | 

2 Answers 2

Reset to default 5

It's not a bug, it's a feature!

http://www.whatwg/specs/web-apps/current-work/multipage/interactive-elements.html#context-menus

User agents may provide means for bypassing the context menu processing model, ensuring that the user can always access the UA's default context menus. For example, the user agent could handle right-clicks that have the Shift key depressed in such a way that it does not fire the contextmenu event and instead always shows the default context menu.

You will not be able to do this in Firefox, by design. It's annoying, especially for plex web apps and games, but it's hard-coded into the browser and there's not way to disable it in javascript (that I know of).

Blame the standards, not Mozilla.

Javascript code to disable mouse right click

 <script language="javascript">
    document.onmousedown=disableRightclick;
    status="Disabled";
    function disableRightclick(event)
    {
      if(event.button==2)
       {
         alert(status);
         return false;    
       }
    }
    </script>

On the HTML Body tag set the oncontextmenu property to false.

<body oncontextmenu="return false">
...
</body>

Disclaimer: The link provided is the blog which i have written. Hope this solves the problem.

发布评论

评论列表(0)

  1. 暂无评论