How to disable right click on the webpage using JavaScript

How to disable right click on the webpage using JavaScript?


To protect the content like images on a web page or to protect the source code or for other security reasons, we need to disable the right click on the web page. Through this blog, I am explaining the procedure to disable the right click with JavaScript.

There are many methods to achieve this. HTML DOM addEventListener() and preventDefault() methods are some of these procedure.

This code will disable the right click by adding an event listener for the "contextmenu event". This code will help you in protecting your web-page.


<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>How to disable right click on webpage using JavaScript</title>
.main-div h1
 font-size: 21px;
   <div class="main-div">
   <h1>Disabling right click on webpage using JavaScript</h1>

<script type="text/javascript">
//Script for disabling right click on mouse
var message="Function Disabled!";
function clickdsb(){
if (event.button==2){
return false;
function clickbsb(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
return false;
if (document.layers){
else if (document.all&&!document.getElementById){

document.oncontextmenu=new Function("alert(message);return false")



Before disabling the Right Click:

After disabling the Right Click:

I am sure this sample code will save your content from getting copied.

Related Topic: How do I disable right click on my web page

Please leave a comment if you find this blog useful. Do subscribe to our website for more solutions

Best WordPress Hosting


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price

Leave a Reply