Controlling right-click using JavaScript

| | Comments (0) | TrackBacks (0)
We've all seen web sites that try dismally to protect their content by disabling the right-click driven context menu appearing. However, you might have a use for using the right-click as a prompt to do something useful. If you do, here's a quick solution:

  <html>
  <body>
  .....
  <script>
  
  function init() {
    var div = document.getElementById('myChoosenDiv');
    div.onmousedown = 
        function (e) {
          var rightclick;
          if (!e) var e = window.event;
          if (e.which) rightclick = (e.which == 3);
          else if (e.button) rightclick = (e.button == 2);
          if (rightclick) doSomething();
          else doSomethingElse();
          return false;
        };
  }

  </script>
  <body oncontextmenu="return false" onload="init()">
  <div id="myChoosenDiv">
    Right click to do something or left 
    click to do something else
  </div>
  
  ...
  </body>
  </html>

Seems to work in IE7 and FF2 on Windows XP

0 TrackBacks

Listed below are links to blogs that reference this entry: Controlling right-click using JavaScript.

TrackBack URL for this entry: http://www.robbiebow.co.uk/mt/mt-tb.cgi/33

Leave a comment

About this Entry

This page contains a single entry by Robbie Bow published on November 14, 2007 11:11 PM.

Some boring facts was the previous entry in this blog.

How to panic a money lender is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.21-en