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.

Note! In order to copy the code from this page, just highlight it using your mouse and press the key combination Ctrl + C or Cmd + C for Mac.

To highlight code using your mouse, position your cursor at the beginning of the code you want to highlight. Press and hold your primary mouse button (commonly the left-button). While holding the mouse button, drag the cursor to the end of the code and let go of the mouse button. Once completed, all code from the beginning to the end should be highlighted.

Simply add the following JavaScript code to the <body> section of your web page:

<script language="JavaScript">
  /**
    * Disable right-click of mouse, F12 key, and save key combinations on page
    * By Arthur Gareginyan (https://www.arthurgareginyan.com)
    * For full source code, visit https://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.

Conclusion

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 :smiley:

Thanks for reading!