thumb

It makes sense to do everything you can to protect your copyrighted content. While there is no way to completely stop people from stealing your images, you can make it harder. Disabling right-clicking is one method you can use to deter casual theft.


On one of my websites, I have a pop-up page where I placed gallery with my copyrighted images. I don’t want the user to see the menu thats get displayed with the mouse right click. So I created the HTML/JavaScript code that will prevent the default right menu from popping up when the right mouse is clicked on this pop-up page. Now I use it to stop surfers from easily stealing my copyrighted images from my website.

Simply add the following JavaScript code to the <body> section of your web page (Press Ctrl C after selecting code to copy it) :

<script language="JavaScript">
  /**
    * Disable right-click of mouse, F12 key, and save key combinations on page
    * By Arthur Gareginyan (arthurgareginyan@gmail.com)
    * For full source code, visit http://www.mycyberuniverse.com
    */
  window.onload = function() {
    document.addEventListener("contextmenu", function(e){
      e.preventDefault();
    }, false);
    document.addEventListener("keydown", function(e) {
    //document.onkeydown = function(e) {
      // "I" key
      if (e.ctrlKey && e.shiftKey && e.keyCode == 73) {
        disabledEvent(e);
      }
      // "J" key
      if (e.ctrlKey && e.shiftKey && e.keyCode == 74) {
        disabledEvent(e);
      }
      // "S" key + macOS
      if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        disabledEvent(e);
      }
      // "U" key
      if (e.ctrlKey && e.keyCode == 85) {
        disabledEvent(e);
      }
      // "F12" key
      if (event.keyCode == 123) {
        disabledEvent(e);
      }
    }, false);
    function disabledEvent(e){
      if (e.stopPropagation){
        e.stopPropagation();
      } else if (window.event){
        window.event.cancelBubble = true;
      }
      e.preventDefault();
      return false;
    }
  };
</script>

This code will block the right click of mouse, Ctrl+ Shift + I, Ctrl+ Shift + J, Ctrl + S, Ctrl + U, and F12 key. The F12 key uses for looking the source code of page, so it also need be disabled.

Also, for addition, you can add the oncontextmenu attribute to the <body> tag of your page HTML code:

<body oncontextmenu="return false;">

Demo

Mouse right click is disabled on this page as well as the Ctrl+ Shift + I, Ctrl+ Shift + J, Ctrl + S, Ctrl + U, and F12 key. You can test it by right clicking anywhere on this page or using one of these key combinations.

Browser support

This method is compatible with main browsers such as Safari and Firefox.

Summarizing

This way I have added the JS blocker of right click of mouse, F12 key, and save combination keys to one of the pages of my website. So simple isn’t it?

If you were able to implement this on your website then please leave a comment.

Thanks for reading!



Arthur Gareginyan

Arthur Gareginyan

Arthur is a designer and full stack software engineer. He is the founder of the studio SpaceX-Chimp and the blog MyCyberUniverse.com. His personal website can be found at arthurgareginyan.com.

Follow Arthur: