-2

I'm developing my website using php. Now I'm tring to disable right click on my web pages using javascript but i don't know how to use the script.

<script type="text/javascript">
$(document).ready(function () {
//Disable cut copy paste
$('body').bind('cut copy paste', function (e) {
    e.preventDefault();
});

//Disable mouse right click
$("body").on("context",function(e){
    return false;
});
});
</script>
Jehy
  • 4,729
  • 1
  • 38
  • 55
devadinesh
  • 137
  • 3
  • 13

3 Answers3

0

Try using the below jquery

$(document).ready(function(){
    $(document).on("contextmenu",function(e){
        if(e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA")
             e.preventDefault();
     });
 });
sujivasagam
  • 1,659
  • 1
  • 14
  • 26
0

You can check with the event like as follows:

$(body).click(function(e){
 if(e.which==1){
   //do the work here this checks for left mouse button.
  }
  });
0
document.addEventListener("mousedown", function(e) {
    if (e.which === 3) { // if e is 1, it is left click, if it is 3 it is 
                         // right click
        alert("right click"); // this will pop up a message saying you 
                              // click the right button


    }
});

In stead of my alert you just use preventDefault like you have in your code above. I just added the alert so you can test for right and left button click to learn more about what is actually happening.

But you are also asking how to use the script. The best way is to put your javacript code in a file with the .js extension, for example main.js and include it in your html file. You can put it at the end of the body tag, to make sure it is loaded after all other html elements are loaded, so that your JavaScript can find them after they are created.

you can put this right before the end of your </body> tag

<script type="text/javascript" src="main.js"></script>

I am assuming you put your javascript in a file by the name main.js

Veracity
  • 49
  • 8