Link to home
Start Free TrialLog in
Avatar of Amanda Watson
Amanda WatsonFlag for Australia

asked on

How can I disable right click on a website

Hi,
I was wondering if someone could tell me the code to use to disable the right click on a webpage.

I know this doesn't protect content, but a client has specifically asked for it.

I tried a few listed here

http://www.dynamicdrive.com/forums/showthread.php?t=5172

but they don't seem to work?
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OR

<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(e)
{
  if(event.button==2)
   {
     alert(status);
     return false;      
   }
}
</script>
function disableContextMenu (element) {
    element.oncontextmenu = function () {
        return false;
    }
}


to apply it simply call this when the page loads on the element you want. (document.body or window would work too.)

disableContextMenu(document.getElementById("myimage"));
fetching the mouse button is a stupid idea since using the mouse is not the only way to show the context menu. thats why you should go with disabling oncontextmenu as i did in my post above.
oh. i forgot that this works too.. you could do it like this aswell:

<body oncontextmenu="return false;">

or something like:

<image oncontextmenu="return false;" src="..." ... / >
you can also use your body tag.

<body class="pageEdit" oncontextmenu="return false">

Open in new window

Avatar of Amanda Watson

ASKER

The first option works just fine thanks
It s a pointless exercise in my opinion anyway, but this is all they wanted
let me quote it again:
fetching the mouse button is a stupid idea since using the mouse is not the only way to show the context menu. thats why you should go with disabling oncontextmenu as i did in my post above.