onclick - disable right click in javascript - Stack Overflow

When I was trying to disable the right click, it is not working.I tried with below code.document.oncli

When I was trying to disable the right click, it is not working. I tried with below code.

document.onclick = function(e) {
  console.log(e.button);
  if(e.button == 2){
    e.preventDefault();
    return false;
  }
}

Based on below link only I tried. Disable Right Click in website

I tested this code in Chrome and Firefox. Every time I am getting the right output only in console. I mean "0" for left click and "2" for right click.But still the default action is happening. May I know the reason.? Thanks in advance.

When I was trying to disable the right click, it is not working. I tried with below code.

document.onclick = function(e) {
  console.log(e.button);
  if(e.button == 2){
    e.preventDefault();
    return false;
  }
}

Based on below link only I tried. Disable Right Click in website

I tested this code in Chrome and Firefox. Every time I am getting the right output only in console. I mean "0" for left click and "2" for right click.But still the default action is happening. May I know the reason.? Thanks in advance.

Share Improve this question edited May 23, 2017 at 12:29 CommunityBot 11 silver badge asked Jun 21, 2015 at 13:48 veerababuveerababu 1551 gold badge4 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You are using wrong event. For right click you should use oncontextmenu. You used onclick which obviously don't fire in right click and hence the loop doesn't evaluate to true in this case.

document.oncontextmenu = function (e) {
    console.log(e.button);
    if (e.button == 2) {
        e.preventDefault();
        return false;
    }

}

Demo: http://jsfiddle/GCu2D/748/

Only three lines of code can disable right click on a web page that is given below:

$("html").on("contextmenu",function(e){
   return false;
});

Source: Close current tab in browser window using JavaScript

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745063250a4609096.html

相关推荐

  • onclick - disable right click in javascript - Stack Overflow

    When I was trying to disable the right click, it is not working.I tried with below code.document.oncli

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信