Location: MST Home / Resources / Tips and Tricks / Right Mouse
CaptureMST Resources
Tips and Tricks
IE 4.0x Right Mouse Capture Script
Capturing the IE 4.0x Right Mouse Button
First, lets look at the code needed to complete the task:
<script type="text/javascript"
language="javascript">
<!--
/*
IE 4.0x Right Mouse Capture Script v1.24
Copyright (C) 1999, MarketSpace Technologies, Inc.
This script may be used on personal,
non-profit and commercial
Websites, so long as this Copyright remain intact and contained
within the script itself. This script may not be sold, whether
separately, or as part of any package, or otherwise transferred
by any means including, but not limited to, digital and print formats,
without the express written permission of MarketSpace Technologies,
Inc.
This script is provided 'AS-IS' and without
warranty of any kind including
the implied warranty of merchantability or fitness for any particular
purpose. For additional information concerning the use of this
script,
please read our Conditions of Use statement which can be found at
http://www.marketspacetech.com/legal.htm.
Please direct all questions and comments
regarding this script, or the
use of this script to info@marketspacetech.com.
*/
if (document.all) {
if (navigator.appVersion.indexOf("MSIE 5")<0) { // Remove
for forms and embedded controls
document.onmousedown=iecapture;
document.onmouseup=iecapture;
document.onclick=iecapture;
document.ondblclick=iecapture;
} // Remove for forms and embedded controls
}
function iecapture() {
var el=event.srcElement;
if (event.button==2) {
if ((el.tagName!="input") &&
(el.tagName!="TEXTAREA") && (el.tagName!="SELECT") &&
(el.tagName!="EMBED") && (el.tagName!="OBJECT")) {
alert('This feature has
been disabled');
}
}
}
// -->
</script>
Now, lets break the code down to make it a little more understandable. First, we
want to make sure that only IE 4.0x or IE 5.0 will utilize this code. We do so by
checking to see if the browser supports document.all,
as this is IE's way of referrencing any object within the page. If the browser isn't
IE 4.0x or IE 5.0, the browser will simply overlook the portion contained within the curly
braces '{ }' and continue on. For example:
if (document.all) {
// IE 4.0x and IE 5.0 specific code goes here
// Other browsers will simply ignore this section of
code.
}
From here, since we have already addressed IE 5.0, we want to make sure that this only
applies to IE 4.0x. We do this by checking the browser version, and if it is a
version less then 5, we'll proceed. To do this, we only need to add in the following
comparison check:
if (navigator.appVersion.indexOf("MSIE
5") < 0) {
// IE 4.0x specific
code goes here
/* We already
determined that this is either IE 4.0x or IE 5.0, with the
the version checking,
we can assume that if this portion of the script
is being run, then this
must be IE 4.0x.
Leave this check out when using Forms, otherwise if you have, also
left out the
oncontextmenu reference, users with the IE 5.0 browser
will still be able to
have the full functionality of Right Clicking. */
}
Next, we want to attach a couple of mouse events to the document, and if those events
occur, we'll run a short function. These mouse events include the MouseDown,
MouseUp, Click and DoubleClick events.
document.onmousedown=iecapture;
document.onmouseup=iecapture;
document.onclick=iecapture;
document.ondblclick=iecapture;
Whenever one of these events occur within the page, the browser will look to see which
function to run. In this case, we have specified that the browser should run the iecapture function. Again, since this section is
contained within the IE 4.0x and IE 5.0 portion of the code, only those browsers will be
aware of the iecapture function.
Next, we'll examine the function that makes it all happen:
function iecapture() {
var el=event.srcElement;
if (event.button==2) {
if ((el.tagName!="input") &&
(el.tagName!="TEXTAREA") && (el.tagName!="SELECT") &&
(el.tagName!="EMBED") && (el.tagName!="OBJECT")) {
alert('This feature has
been disabled.');
}
}
}
We start off by utilizing the variable el to take the place of the much longer
event.srcElement reference. This simply helps to shorten our code later.
var el=event.srcElement;
The next task is to determine which button is being clicked. IE allows us to
differentiate between browser buttons by using event.button==2 since 2 is the reference to
the Right Mouse button.
var el=event.srcElement;
if (event.button==2) {
// Right Mouse button
was clicked
}
There are certain situations where using the Right Mouse button is necessary, and is
expected. A perfect example of this type of situation would be with forms where
cut/copy and paste may be used to populate various form elements, such as a text field.
To make sure that our visitors are capable of utilizing forms within our site in an
expected fashion, we simply incorporate a check to determine if the mouse event occured on
one of these elements.
if
((el.tagName!="input") && (el.tagName!="TEXTAREA") &&
(el.tagName!="SELECT") && (el.tagName!="EMBED") &&
(el.tagName!="OBJECT")) {
We want to exclude input boxes since these, within a form, these objects make up text
fields and password boxes. We'll, also, want to exclude TEXTAREA since this can
typically contain multiple lines of text. There is no logical reason, that we can
find, why you would want to force your visitors to type everything into a text area, when
in many cases, they can simply copy and paste. We, also, incorporate EMBED and
OBJECT since some embedded controls offer features only available through a Right Mouse
click on the control itself.
IMPORTANT: Remember, the goal here is to make it more difficult
to view the source, or save images directly off of the page... It isn't to make our
site more difficult for the person wishing to make a purchase, or convey how much they
like what we have done with our site! We, also, don't want to prevent the ability to
use embedded controls, such as controls for playing audio files. Keep it simple and
functional for the honest viewer while increasing the level of difficulty of the
dishonest!
Finally, if the browser is IE, the version is 4.0x, the Right Mouse button was clicked,
but not on an input, textarea or embedded control, we'll capture the Right Mouse
button. One pitfall that we ran into while developing this script was in the
apparent realization that IE 4.0x doesn't allow for the absolute overriding of the default
behavior, which happens to be the displaying of the context menu. We found that, in
order to override the default behavior, we had to supply IE 4.0x with another
behavior. There are a number of ways of accomplishing this, however, we decided to
go with a simple, but effective, means in which to accomplish this task. We simply
put up a Javascript alert indicating that the feature has been disabled.
alert('This feature has been disabled.');
Previously, we prompted to close the browser window. While this was a very
effective means in which to perform the task, it wasn't quite ideal. We didn't want
our guests to leave, we just wanted to prevent their ability to use the default behavior,
which is the display of the context menu. Finally, let's move on to NS 4.0x now.
<< Back ][ Next
>>
[ Top of Page ] |