Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q969 How can I suppress a right mouse click?

You are here: irt.org | FAQ | JavaScript | Pointer | Q969 [ previous next ]

This script is not fool-proof. People can disable JavaScript support in their browser, or use a browser which doesn't support JavaScript, or use a browser to link directly to the image that you want to protect. Also the pages and images are normally stored in the users browser cache - for viewing later whilst offline. It also doesn't stop users browsing the source of youe page from the toolbar menus. You could attempt to load you page into a window without toolbars - but again this isn't fool-proof (for the already listed reasons). There is tool for Microsoft Internet Explorer 5 that will encrypt JScript code - but this is only supported by Microsoft Internet Explorer 5 - and will not allow the scripts to run on any other browser.

Bearing this in mind the following script is an enhanced version of the first one. It _hopefully_ stops right clicks (and left button held down, followed by a right click) in both Netscape Navigator and Microsoft Internet Explorer. It will not stop users who use a Mac - where the mouse only has one button.

It this script doesn't do what you require (i.e. protect your images and or your scripts) then don't use it and don't put your images/scripts on the net. If your livelyhood relies on people not copying your images - then add a water mark or "sample" stamp to them.

This first example requires an alert message to work correctly in Internet Explorer:

<head>

<script language="JavaScript1.1"><!--
var debug = true;
function right(e) {
  if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2)) return false;
  else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2 || event.button == 3)) {
    alert('Denied!');
    return false;
  }

  return true;
}
document.onmousedown=right;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
window.onmousedown=right;
//--></script>

</head>

<body>

<!-- place your content after this line -->

<p>
<img src="picture.gif" width="100" height="100">
</p>

<p>
some text
</p>

<p>
<a href="somewhere.htm">text link</a>
</p>

<!-- and before this line -->

<script  language="JavaScript1.1"><!--
// to prevent right click on images include:
for (var i=0; i<document.images.length; i++) document.images[i].onmousedown=right;

// to prevent right click on links include:
for (var i=0; i<document.links.length; i++) document.links[i].onmousedown=right;
//--></script>

</body>
</html>

Andrei Druzhinsky reponds with:

The following (not mine) disables the right click button totally - no messages are seen. You have to download ActiveX though from Microsoft site, which literally takes few seconds.

<head>

<script language="JavaScript"><!--
menuon=false;

function cancelmenu(e) {
  if (document.all) {
    if (event.button == 2) {
      menuon=true;
      if (popmenu.readyState==4) {
        popmenu.RemoveAllItems();
        popmenu.AddItem("","","","");
        popmenu.Popup (event.x,event.y);
      }
      else
        alert("Please wait. Page Loading...")
      }
      if (event.button == 1) {
        if (menuon) {
          popmenu.RemoveAllItems();
          popmenu.Popup (event.x,event.y);
          menuon=false;
        }
      }
    }
  }
}
document.onmousedown = cancelmenu;
//--></script>

</head>

<body>

<object id="popmenu" width=0 height=0 classid="CLSID:F5131C24-E56D-11CF-B78A-444553540000" codebase="http://activex.microsoft.com/controls/iptdweb/ikcntrls.cab">
<param name="_Version" value="65536">
<param name="_ExtentX" value="2646">
<param name="_ExtentY" value="1341">
<param name="_StockProps" value="0">
</object>

</body>

</html>

This last example works without an alert message in Internet Explorer:

<script language="JavaScript1.2">
if (window.Event)
  document.captureEvents(Event.MOUSEUP);

function nocontextmenu() {
  event.cancelBubble = true, event.returnValue = false;

  return false;
}

function norightclick(e) {
  if (window.Event) {
    if (e.which == 2 || e.which == 3) return false;
  }
  else if (event.button == 2 || event.button == 3) {
    event.cancelBubble = true, event.returnValue = false;
    return false;
  }
}

if (document.layers)
  document.captureEvents(Event.MOUSEDOWN);

document.oncontextmenu = nocontextmenu;
document.onmousedown = norightclick;
document.onmouseup = norightclick;
//--></script>

Note: On a Windows keyboard there is a "right click" key which simulates a mouse right click. (Located next to the right hand "Ctrl" key) The methods detailed in Q969 supress the mouse right click but not the keyboard simulation of a right click. This is the context menu and not actually a right click. IE5+ should understand this:

<body onContextMenu="return false">

©2018 Martin Webb