Click here to Skip to main content
Licence CPOL
First Posted 2 Oct 2009
Views 10,156
Bookmarked 27 times

How to Restrict Mouse Right Click on Web Page

By | 2 Oct 2009 | Article
Simple technique to restrict the mouse right click event on ASP.NET web page

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

//******************************************************************************
// Module  :   rightClick.js
//******************************************************************************
// DISCLAIMER: This application is provided on AS IS basis without any warranty
//******************************************************************************
var BM = 2; // button middle
var BR = 3; // button right
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DrABELL

Chief Technology Officer
Infosoft Int'l
United States United States

Member

Dr. A. Bell (aka DrABELL), hi-tech consultant living in the US, has more than 20 years of software and electronic engineering experience. He published more than 100 technical articles and authored 37 inventions. Dr. Bell is the Windows/Internet technologies veteran, currently focused on: .NET,Mobile,Java,C#,SQL,HTML5,CSS3. He developed the most popular Silverlight Media Player (#1 Goog) and 3 Fractions Calculator (also #1 on Google/Bing/Yahoo). Sample online projects:
  1. Embedded youTube Player: ASP.NET API
  2. Semantic Analyzer
Popular hi-tech articles:
  1. WebTV Project: Embedded YouTube Player
  2. Personal computer 2011: mostly USB 3.0, SATA 3.0, DDR 3 plus SSD/HDMI
  3. Introducing the unit of internet social network efficiency: 1 Zuck
  4. Internet leader 2010: Facebook. See the entire Top10 list
  5. How to select web browser and check its capabilities
  6. SQL generates large data sequence
  7. Aggregate Product function extends SQL
  8. HTML 5, CSS 3 and Inflation Calculator
  9. RIA: embedding YouTube
  10. RIA: Silverlight™ media player
  11. RIA: HTML 5 video player


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 Pinmembermandar130522:38 4 Aug '11  
GeneralMy vote of 5 PinmemberMember 47753204:18 8 Jan '11  
QuestionIneffective in an HTML file? PinmemberGawiz4:53 6 Oct '09  
GeneralMy vote of 1 PinmemberFahad Sadah4:40 6 Oct '09  
GeneralOpera PinmemberMikeSamteladze21:11 5 Oct '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 2 Oct 2009
Article Copyright 2009 by DrABELL
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid