Latest Technology News, Mobile, Laptop, Gadgets and Much More

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

1 1,274

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

Today in this short article I will discuss on how to protect website content from being copied, mean’s in this article I will show you how to disable mouse right click and keyboard F12 key using javascript. For stopping the content copy of your website you can do two things, one is to disable the mouse right click and second is to disable the cut (CTRL+X), copy (CTRL+C) and paste (CTRL+V). Using of javascript, you can easily disable mouse right click and disable cutcopy and paste from web content.

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

Just copy and paste below code into your website head section .if your website in WordPress, so goto Appearance, Editor and find the header.php file and paste below code above </head>.

Disable Mouse Right Click

Disable mouse right click will prevent visitors from choosing the cut, copy and paste options. If you want to disable right mouse click for a particular section of a web page, then you can use a selector (#id.classetc.) otherwise use the bodyselector for the full page. The following JavaScript code is used to disable mouse right click.

<script type="text/javascript">
$(document).ready(function () {
    //Disable full page
    $("body").on("contextmenu",function(e){
        return false;
    });
    
    //Disable part of page
    $("#id").on("contextmenu",function(e){
        return false;
    });
});
</script>

Disable Cut Copy & Paste

The following JavaScript code will disable cut, copy and paste from your web page or a part of your web page.

<script type="text/javascript">
$(document).ready(function () {
    //Disable full page
    $('body').bind('cut copy paste', function (e) {
        e.preventDefault();
    });
    
    //Disable part of page
    $('#id').bind('cut copy paste', function (e) {
        e.preventDefault();
    });
});
</script>

Disable Mouse Right Click, Cut, Copy & Paste

Full JavaScript code for disabling mouse right click and disable cut (CTRL+X), copy (CTRL+C) and paste (CTRL+V) from the web page.

<script type="text/javascript">
$(document).ready(function () {
    //Disable cut copy paste
    $('body').bind('cut copy paste', function (e) {
        e.preventDefault();
    });
   
    //Disable mouse right click
    $("body").on("contextmenu",function(e){
        return false;
    });
});
</script>

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More