How To Disable Right Click & F12 Key on a Website Using Javascript

Disable Right Click & F12 Key on a Website Using Javascript

Disable Right Click & F12 Key on a Website Using JavaScript is so simple that any person with little knowledge about programming and JavaScript can easily insect codes and disable right clicks on his/her website and blogs. In this article, we are going to discuss how to stop Right Click of the mouse, f12 keys on your website.

Why Disable Right Click & F12 Key on a Website?

In InspiritLive we have not disabled right click and F12 key for another reason.

It is almost necessary for a blogger to protect his content from being copied in the field of blogging. Continue the blogging profession is more important than just starting a blog and leave it soon.

Important:

Just copy and paste the below code into your website head section. If your website in WordPress, just install a plugin – Insert header and Footer and paste the below code above in the Head section. If you are using any other platform then find the </head> section and paste the below code just above the </head>.

JavaScript to Disable Right Click & F12 Key

<script language="JavaScript">
      
       window.onload = function () {
           document.addEventListener("contextmenu", function (e) {
               e.preventDefault();
           }, false);
           document.addEventListener("keydown", function (e) {
               //document.onkeydown = function(e) {
               // "F12" key
               if (event.keyCode == 123) {
                   disabledEvent(e);
               }
               // "U" key
               if (e.ctrlKey && e.keyCode == 85) {
                   disabledEvent(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);
               }
                              
           }, false);
           function disabledEvent(e) {
               if (e.stopPropagation) {
                   e.stopPropagation();
               } else if (window.event) {
                   window.event.cancelBubble = true;
               }
               e.preventDefault();
               return false;
           }
       }
//edit: removed ";" from last "}" because of javascript error
</script>

With the help of the above code, we will be able to disable the use of the Right Click by preventDefault() method in contextmenu event. It will also disable the F12 , Ctrl + U, Ctrl + Shift + I, Ctrl + Shift + J and Ctrl+S keys.

We hope this article helped you learn how to easily disable right click and F12 Key on your website and also helps you to protect your content from being copied.

Leave a Comment