Introduction
To make the ASP.NET web-page look-n-feel more "application-like", it is sometimes useful to restrict the right mouse click event on the page (see the working demo at www.webinfocentral.com).
Background
This project has both practical and didactic aspects. It is presented in a simple "How To" form, containing just two components:
- Javascript module (rightClick.js)
- Sample ASP.NET Web page (Default.aspx) to demonstrate the functionality
Using the Code
Create two files: javascript.js and Default.aspx (or any other .aspx file named up to your discretion) and place them in the ASP.NET application root directory. Copy the code snippets shown below, correspondingly.
JavaScript File
var BM = 2; var BR = 3; var msg ="MOUSE RIGHT CLICK IS NOT SUPPORTED ON THIS PAGE";
function mouseDown(e)
{
try { if (event.button==BM||event.button==BR) {return false;} }
catch (e) { if (e.which == BR) {return false;} }
}
document.oncontextmenu = function() { alert(msg); return false; }
document.onmousedown = mouseDown;
Default.aspx File
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>NO RIGHT CLICK | DEMO</title>
<script src="rightClick.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
MOUSE RIGHT CLICK EVENT IS NOT SUPPORTED (TRY IT)...
</div>
</form>
</body>
</html>
Points of Interest
The solution has been tested to work with four major browsers:
- Internet Explorer 6.0/7.0/8.0
- FireFox 2.0/3.0
- Safari
- Chrome
History
- Article posted on 09/14/2009