Home >>Javascript Programs >Disable right click on webpage with JavaScript

Disable right click on webpage with JavaScript

Disable right click on webpage with JavaScript

Sometimes we want to disable right click on the web page like right-clicking and copying text, to see inspect element by right-clicking. To disable right click on the web page we have to use JavaScript code.

In this tutorial we will discuss how to disable right click on the webpage by using javascript

Let's take an example

<!DOCTYPE HTML> 
<html> 
	<head> 
		<title> 
			Disable right click on my web page 
		</title> 
	</head> 
	<body style = "text-align:center;"> 
		<h1 style = "color:green;" > 
			Phptpoint 
		</h1> 
		<p id = "PHP_UP" style = "font-size: 16px; font-weight: bold;"> 
		</p> 
		<button onclick = "PHP_Run()"> 
			Disable 
		</button> 
		<p id = "PHP_DOWN" style = 
			"color:green; font-size: 20px; font-weight: bold;"> 
		</p> 
		<script> 
			var el_up = document.getElementById("PHP_UP"); 
			var el_down = document.getElementById("PHP_DOWN"); 
			el_up.innerHTML = "Click on the button to disable right click"; 
			
			function PHP_Run() { 
				document.addEventListener('contextmenu', 
						event => event.preventDefault()); 
				el_down.innerHTML = "Right click disabled"; 
			}		 
		</script> 
	</body> 
</html>					 					 
Output:

Phptpoint