disable right click on web page

Learn how to disable right click on web page using Java script with example program

Note:

Click is a mouse event which we want to disable.

below is the syntax for addEventListener() Method

syntax

document.addEventListener(event, function)

Parameters:

event: It specifies name of the event.
It is a mandatory parameter. event name should in string format(double quotes).

function: It specifies the function to run when the event occurs.
It is a mandatory parameter. When the event occurs, an e vent object is passed as the first parameter to the function.
The type of the event object depends on the defined event. For example, the “click” event is of Mouse Event object.

to disable any event we use preventDefault() Event Method

preventDefault() Event Method: This method cancels the event if it can be cancelled, meaning that it stops the default action that belongs to the event.

Example program to disable right click event.

This example disable the right click by adding an event listener for the “contextmenu” event and calling the preventDefault() method

<!DOCTYPE HTML> 
<html> 
	<head> 
		<title> 
			Disable right click on my web page 
		</title> 
	</head> 
	
	<body  onload="gfg_Run()"> 
		
		<p> sample Program to disable Right click on the web page </p>
		Email: <input type="textbox" id="email" ><br/>
                Confirm Email:    <input type="textbox" id="confirmEmail">				
		
		<script> 
						
			function gfg_Run() { 
				document.addEventListener('contextmenu', 
						event => event.preventDefault()); 
				el_down.innerHTML = "Right click disabled"; 
			}		 
		</script> 
	</body> 
</html>					 


Leave a Comment