Amanda Watson
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(documen t.getEleme ntById("my image"));
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(documen
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="..." ... / >
<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">
ASKER
The first option works just fine thanks
It s a pointless exercise in my opinion anyway, but this is all they wanted
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.
<script language="javascript">
document.onmousedown=disab
status="Right Click Disabled";
Function disableclick(e)
{
if(event.button==2)
{
alert(status);
return false;
}
}
</script>