1

Right now I'm using this code to disable right click :

$("body")
      .attr('ondragstart', 'return false')
      .attr('onselectstart', 'return false');

    var message='Right click is disabled on this page!';
    function clickIE4() {
      if (event.button==2) {
        alert(message);
        return false;
      }
    }

    function clickNS4(e) {
      if (document.layers||document.getElementById&&!document.all) {
        if (e.which==2||e.which==3) {
          alert(message);
          return false;
        }
      }
    }

    if (document.layers) {
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown=clickNS4;
    } else if (document.all&&!document.getElementById) {
      document.onmousedown=clickIE4;
    }
    document.oncontextmenu = new Function('alert(message); return false');

Now I have implemented context menu of my own and I'm displaying it when clicked on certain a element with class .context.

I'm using http://medialize.github.io/jQuery-contextMenu/demo.html

So I'd like to disable all right click context menus except the one with .context class

3
  • 3
    You must have copy-pasted this from somewhere that hasn't been updated in something like 10 years Commented Jul 2, 2013 at 15:10
  • 3
    There is really no foolproof way to disable right-clicks. Anyone can view your source and take whatever they want. Or even open it up in Firebug or another program and disable the scripts in real time. It won't accomplish much except to irritate your users. Commented Jul 2, 2013 at 15:13
  • 1
    I agree, it´s a very bad practice from an usability point of view. You gain absolutely nothing except from an maybe annoyed user. Commented Jul 2, 2013 at 15:26

2 Answers 2

1

You can try something like:

 $('body *:not(.context)').on('contextmenu', function (evt) {
     evt.preventDefault();
 });

EDIT: This seems to work better:

 $(document).on('contextmenu', function (evt) {
     if (!$(evt.target).is('.context')) {
         evt.preventDefault();
     }   
 });
Sign up to request clarification or add additional context in comments.

Comments

0

Use this snippet:

var message="Right click is disabled on this page!";
jQuery("*").on("contextmenu", function(e) {
    e.stopPropagation();
    if ($(this).hasClass("context")) {
        alert("Opening custom right-click menu.");
    }
    else {
        alert(message);
    }
    return false;
});

See a demo here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.