Prevent debugging of a Website using Javascript

Prevent Debug

 

In this blog I am going to show you how to prevent debugging of your website by using Javascript. For this simple trick you need to know basic HTML and Javascript language.

First of all we have to write a code that disables all the ways to access Inspect Element as well as to view the source code and to prevent Cut, Copy and Paste in JavaScript. We will prevent the following.

  • Right Click
  • Ctrl + Shift + I
  • Ctrl + Shift + J
  • Ctrl + Shift + C
  • Ctrl + U
  • F12

 

Disable Right Click

 

//this will also disable full page

$("body").on("contextmenu",function(e){
 return false;
});

 

Disable Cut, Copy, Paste

 

$('body').bind('cut copy paste', function (e) {
    e.preventDefault();
});

 

Prevent Debugging the Website

 

document.onkeydown = function(e) {
    
    //prevent key F12
    if(event.keyCode == 123) {
        return false;
    }

    //prevent CTRL + Shift + I
    if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)){
        return false;
    }

    //prevent CTRL + Shift + J
    if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)){
        return false;
    }
    //prevent CTRL + Shift + C
    if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)){
        return false;
    }
    //prevent CTRL + U 
    if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)){ 
        return false; 
    } 
}

 

That’s it. You will now have a website with Debug prevent. Keep coding!

Please share if you like it!


Preetam Mallick