home

Search A2Z 24

Disable right mouse click(right-click) on a web page using javascript/jQuery

X

Disable Right-Click Features

  • It will protect your contents along with the pictures. Anyone who will try to copy, get your clever wink ;)
  • It will not foray on Pin it Button if applied to images.
  • It is good for those people, who share the information only, but do not permit to use their stuff.
  • It can be a trouble for those people, who provide technique and tricks over the particular topic and display codes (like HTML, JavaScript etc.) for others so that they can copy for their use.
  • You cannot open a link in another tab in a browser where right click is disabled.

Protect images/Disabling right-click on a web page,Below mention code(JS) to prevent the vast majority of users from easily saving your web page, viewing its source, or lifting images off your site or right-clicking over an image and saving it when using either IE 4+ or NS 4+.

There are many ways to view the code, though.
This is not always a wise technique to use - it would have to be a very special piece of code to want to hide, and those who would want to steal it will always be able to find a way to see it.

This is a cross browser DHTML script that will prevent the default right menu from popping up when the right mouse is clicked on the web page. 
Definitely useful for many websites.

<script>
//Disable right mouse click Script
message="Right click is disable on this website!";

function clickIE4(e){      
	if (e.button==2){
		alert(message+'  IE');
		return false;
	}	        
}
function clickNS4(e){       
	if (document.layers||document.getElementById && !document.all){
		if (e.which==2||e.which==3){
			alert(message+'  NS');
			return false;
		}
	}        
}
//From a period where Internet Explorer 4 and Netscape 4.x were the main browsers 
//document.layers was used by Netscape, and document.all from Internet Explorer. 
//The first is definitely unused anymore, where document.all is still used for legacy in IEs. 
//document.all and document.layers are not part of the W3C DOM So 
//Today's we use document.getElementById instead.

if (document.layers){ 
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS4;        
}
else if(document.all && !document.getElementById){ 		
	document.onmousedown = clickIE4;        
}	
document.oncontextmenu=function(){alert(message);return false}
</script>

Disable Right Click Using jQuery

Their are large number of JavaScript snippets which allows you to disable Right Click on Webpage. But, using jQuery you can perform this task with just few lines of code.
The following jQuery code allows you to quickly disable Right Click on your webpage.

<script>
$(document).ready(function(e) {
	$(document).on('contextmenu',function(e){
		alert("Right click is disable on this website!");
		return false;
	});
});
</script>

The above code will disable right click on entire document.

To disable right click on specific elements you can use respective selectors.
The following code will disable right click only on images which are being displayed using tag.

<script>
$(document).ready(function(e) {
	$('img').on('contextmenu',function(e){
		alert("Right Click is not allowed on this website!");
		return false;
	});
});
</script>

About Author

by Admin

Share your thoughts!

Login as a member to access comment posting block !! click-here

Thoughts From Other Users (0)

No Comments

×