0

I am currently using this script to prevent right click on one of my site. I have tried different tweaks on the code because I would like it to prevent right click ONLY on images (right click it's everywhere).

Any idea?

Thanks in advance for your help

//Disable right mouse click Script

var message = "Stop trying stealing pics! :P";

///////////////////////////////////
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")
mplungjan
  • 155,085
  • 27
  • 166
  • 222
Y. A.
  • 19
  • 1
  • 4

1 Answers1

5

Use contextmenu event:

$(document).ready(function() {
     $("img").on("contextmenu",function(){
        return false;
     }); 
 });
img{
  width: 50px;
  height: 50px;
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded">
SilverSurfer
  • 4,094
  • 5
  • 20
  • 42