How do I disable right click on my web page?
in
javascript
- on 7:05 PM
Introduction
Want to protect your source code? Using this code will prevent the vast majority of users from right-clicking over a page and choosing "View source", or right-clicking over an image and saving it.
Some times there might be the requirement to disable the pop up menu on click of right button.
Programmers may need to disable right click to prevent user from saving the images on the page or viewing the source of the page.
Though disabling right click is not complete solution to save the data, but it will make task difficult, or may be impossible for the rookies.
I searched on a net for the solution to this problem and i got following solution.
Step1
In this method we add a javascript method, in this we check if click is right click or left click if it is right click then a message is displayed like "Right click disabled"
+code
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
function disableclick(event)
{
if(event.button==2)
{
alert(status);
return false;
}
}
</script>
But in this solution if you click the right click it displays the message
“Right click disabled”.
If we want to remove the messegebox then this solution do not work.
I am having another simple solution to achieve the same result and also it does not show the messegebox.
Step 2
In this method we set the oncontextmenu="return false" in the body part of the page
so body part will look like
+code
<body oncontextmenu="return false">
...
</body>

