Quote:
> Hi,
> On my page I use the following script to disable the right mouse click
> The question : Is it possible that the user sees nothing when (s)he
> rightclicks instead of an alertbox ?
Let me first point out that disabling the right mousebutton in absolutely no way will hide your
source code!
With that out of the way, let's go to work:
The utterly annoying alert box is only neccessary in IE 4.* on Windows
systems, as IE 5.0+ has the document.oncontextmenu event, specifically designed
for blocking the contextmenu, and Netscape 4.* allows you to quietly cancel the onmousedown event.
So let's build up a working copy of blocking the contextmenu and extend it's
functionality to handle these different cases.
<SCRIPT language=JavaScript>
<!--
if (document.all){ // We have IE 4+
if (window.print){ // We have IE5+
document.oncontextmenu = function(){
if (event.srcElement.tagName=="BODY") return false;
}
} else { // We have IE4.*
document.onmousedown = function(){
if (event.srcElement.tagName=="BODY" && event.button!=1){
if (navigator.platform.indexOf("Win")==0){ // We have Windows
alert('Sorry, the right mouse button has been blocked !');
}
return false;
}
}
}
Quote:
}
if (document.layers){ // We have Netscape 4.*
window.captureEvents(Event.MOUSEDOWN);
window.onmousedown = function(e){
if(e.target==document && e.which!=1) return false;
}
Quote:
}
// -->
</SCRIPT>
Now we have a working copy of a script that blocks the contextmenu, and only
gives the user an alert box when completely neccessary. Furthermore, the user is still allowed to
right-click on images, links and so forth.
Some more lines added, and we would have a W3C DOM compliant
contextmenu-blocking script (using addEventListener() ).
Remember to check the FAQ or do a search on Deja before posting questions, this is a rather common
question!
--
Thor Larholm