www.gutterbucket.com | |||||||
Home | Software | Java | JS/HTML | General | Perl/CGI | Links | Sitemap |
If your a very paranoid person and want to hide your scripts from visitors because you don't want them to steal your work then read on.
Because the Event object is implemented differently by Navigator and IE the handler must be designed to work for both. I have provided an example solution below.
01 <head> 02 <title>Protect Your Code !</title> 03 <script language=javascript type="text/javascript"> 04 <!-- 05 function one4allHandler(nsevent) { 06 if (window.event) { 07 if (window.event.button == 2) { 08 alert('Your Message !!!'); 09 } 10 } else { 11 if (nsevent.which == 3) { 12 return false; 13 } 14 } 15 } 16 17 if (document.captureEvents) { 18 document.captureEvents(Event.MOUSEDOWN); 19 } 20 document.onmousedown = one4allHandler; 21 //--> 22 </script> 23 ... 24 </head>
Line 20 installs the event-handler and 17-19 are used to force Navigator to capture all MouseDown events even if they occur on an image, a link or just some text.
The first thing that our event handler does is to check for
the existence of window.event
. This tells us if
the browser uses an IE style Event object. We then check to
see if the right button was pressed (event.button==2
in IE or event.which==3
in Navigator).
In Navigator we can return a value of false and the menu will not be displayed. This approach however will not work for IE, instead we must display an alert message to knock it off its feet.
Under IE5 the following code will prevent the context menu from being displayed.
01 <SCRIPT LANGUAGE=JavaScript> 02 <!-- 03 function ie5cmHandler() 04 { 05 event.returnValue = false; 06 } 07 document.oncontextmenu = ie5cmHandler; 08 //--> 09 </SCRIPT>
That's it !!! Simple when you know how.