|
Right Click
Disabling the Right Click function of a browser can be fun. The objects and methods the browser uses to allow things such as a right click can be accessed directly in a few cases. This is one of them. You can use this function to limit what the user is allowed to do. This is handy if you have an embedded video file and you don’t want to offer the user the chance of saving it to their computer’s hard drive or directly to an Internet URL.
<html>
<head>
<script type="text/javascript">
document.onmousedown=disable; //IE
message="Right Click functions have been disabled.\nNow you cannot view my source\n and you cannot steal my images";
function disable(e) {
if (e == null) { //IE disable
e = window.event;
if (e.button==2) {
alert(message);
return false;
}
}
document.onclick=ffdisable; //FF
}
function ffdisable(e) {
if (e.button==2) { //firefox disable
e.preventDefault();
e.stopPropagation();
alert(message);
return false;
}
}
</script>
</head>
<body>
<p>Right-click on this page to trigger the event.</p>
<p>Note that this does not guarantee that someone won't view the page source or steal the images.</p>
</body>
</html>
v
Note the addition of an extra line of instructions to provide for the Internet Explorer function. It’s always a good idea to provide for older browsers. Cross platform and cross version compatibility are extremely important. When the user uses the right mouse button, the first directive on the first line is consulted: an onMouseDown event. Provision for FireFox has been included, using the preventDefault and stopPropagation directives. After any of the directives has been completed, a status of false is returned and the contents of the message variable are displayed in an alert box.
|
|