///Disabling the Right-click Mouse Button

Disabling the Right-click Mouse Button

Disabling the Right-click Mouse Button

Sometimes you don’t want the visitor to be able to click the right mouse button and do all the stuff associated with it. Although personally I’m not in favor of such gimmicks and limit the visitor’s independence, you learn something new in this tutorial.

<script language=JavaScript>
var IE;
var NN;

if(navigator.appName=="Microsoft Internet Explorer")
{
IE=true;
}

if(navigator.appName=="Netscape")
{
NN=true;
}

The above code checks what browser the user is using and store the information accordingly. Both the browsers use different class variable names to store different events.

function right(click)
{
if(IE && (event.button==2 || event.button==3))
{
alert("The right click has been disabled here.");
return false;
}

if(NN && (click.which==2 || click.which==3))
{
alert("The right click has been disabled here.");
return false;
}

return false;
}

And the code below captures the event and sends it to the browser.

if (document.layers) window.captureEvents(Event.MOUSEDOWN);
if (document.layers) window.captureEvents(Event.MOUSEUP);
document.onmousedown=right;
document.onmouseup=right;
window.document.layers=right;
</script>

2010-05-26T11:00:10+00:00 June 1st, 2005|JavaScript|0 Comments

About the Author:

Amrit Hallan is a freelance web developer. You can checkout his website at http://www.bytesworth.com. For more such articles join BYTESWORTH REACHOUT at http://www.bytesworth.com/br/default.asp or if you have a web dev related question then post it at http://www.business180.com/index.php

Leave A Comment