Thursday 6 March 2014

How to disable mouse right click in HTML

Method 1:

Easiest way of disabling the mouse right click in the websites is by just adding the "oncontextmenu" attribute in  body tag.


Example:
<html>
<body  oncontextmenu="return false">
<h1>Disabling the Mouse Right Click in HTML.</h1>
<p>
This is a  test paragraph for to demonstrate the disabling of mouse right click.
<em color="red">Right Click on me.</em></p>
</body>
</html>

 Note:
If you don't want to disable the context menu on the whole page and if you want to disable a single element in html you need to write the oncontextmenu="return false" attribute in that particular tag.


Method 2:

Here is another way of disabling the mouse right click in the websites is by adding some javascript in the script tag. In this method you can give the alert to the user saying that the "Mouse right click is disabled in this page".

<html>
<head>
<script>
document.oncontextmenu = function() {
  alert('Mouse right click is disabled on this page!') ; 

}
</script>
</head>
<body>
<h1>Disabling the Mouse Right Click in HTML.</h1>
<p>
This is a  test paragraph for to demonstrate the disabling of mouse right click.
<em color="red">Right Click on me.</em></p>
</body>
</html>





No comments: