www.gutterbucket.com

Protect Your Code !

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.


Ways to Protect your scripts

Use frames
If the file containing the code is placed in a frameset then the view-source option on the menubar will only show the frameset's code.
Disable Right-Click menu
The view-source option on the menubar is usually also available via a right-click popup menu. It is however possible to disable this feature by setting up a handler for the document's onmousedown event (see example code below).
Use External .js files
If the user does use the view-source option then less of your code is visible.
Get Flash
The best way to protect your html is to not use it at all. Instead spend a fortune on Flash 5 and use that to create your site.

How to Disable Right-Click Menu (IE 4 / NAV 4)

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.


Copyright 2000-2007  Mark R Hives  -  All rights reserved