Disabling copy and right-clicking by using JavaScript

Disabling copy and right-clicking by using JavaScript

Disabling copy and right-clicking by using JavaScript

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

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.

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

  1. <script language="JavaScript">
  2.   /**
  3.     * Disable Copy ad right-click of the mouse, F12 key, and save key combinations on page
  4.     * For full source code, visit https://onlylearn24.com/
  5.     */
  6.   window.onload = function() {
  7.     document.addEventListener("contextmenu", function(e){
  8.       e.preventDefault();
  9.     }, false);
  10.     document.addEventListener("keydown", function(e) {
  11.     //document.onkeydown = function(e) {
  12.       // "J" key, For full source code, visit https://onlylearn24.com/
  13.       if (e.ctrlKey && e.shiftKey && e.keyCode == 74) {
  14.         disabledEvent(e);
  15.       }
  16.       // "I" key, For full source code, visit https://onlylearn24.com/
  17.       if (e.ctrlKey && e.shiftKey && e.keyCode == 73) {
  18.         disabledEvent(e);
  19.       }
  20.       // "F12" key, For full source code, visit https://onlylearn24.com/
  21.       if (event.keyCode == 123) {
  22.         disabledEvent(e);
  23.       }
  24.       // "U" key, For full source code, visit https://onlylearn24.com/
  25.       if (e.ctrlKey && e.keyCode == 85) {
  26.         disabledEvent(e);
  27.       }
  28.       // "S" key + macOS, For full source code, visit https://onlylearn24.com/
  29.       if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
  30.         disabledEvent(e);
  31.       }
  32.     }, false);
  33.     function disabledEvent(e){
  34.       if (e.stopPropagation){
  35.         e.stopPropagation();
  36.       } else if (window.event){
  37.         window.event.cancelBubble = true;
  38.       }
  39.       e.preventDefault();
  40.       return false;
  41.     }
  42.   };
  43. </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 at the source code of the page, so it also needs to be disabled.

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

  1. <body oncontextmenu="return false;">

All Disabling copy and right-clicking

  1. <body ondragstart="return false;" onselectstart="return false;"  oncontextmenu="return false;" onload="clearData();" onblur="clearData();">

Browser support:

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

 

 

Thank You.......................