View Full Version : disabling right clicks

06-28-2004, 10:47 PM

I saw quite a few scripts that allow the user to disable the right-click on certain components (usually images).

This is what the code looked like (for IE);

function disableRightClick() {
if (event.button == 2) {
alert("Sorry, right-click is disabled");
return false;

Then call this function like so:

<a href="test.html" onmousedown="disableRightClick();">test</a>

I would like to disable right-clicks without the annoying alert.
I just don't want the menu to open when the user right-clicks the link.
Is that possible?
When I remove the call to alert, then the code has no effect.


06-28-2004, 11:16 PM
<a href="test.html" oncontextmenu="return false;">test</a>

Not sure how compliant that is.

06-28-2004, 11:18 PM
The reason it stops working when you remove the alert() command is that the alert box takes the focus away from the menu. I dunno if you can get it to work silently. I do have to refer you nonetheless to see the sticky on disabling right click functionality (http://www.codingforums.com/showthread.php?t=4437) just in case you weren't aware.

06-28-2004, 11:39 PM
thanks for the help

06-29-2004, 09:06 AM
Are you aware that anyone can simply select (highlight) text or images with the left button and then hit Control+c to copy it to their clipboard?

The following works a lot better, but I'm sure some expert can find a way around it:

var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers)
document.oncontextmenu=new Function("return false")

<body onselectstart="return false">

06-29-2004, 12:37 PM
FYI, it is best to place code inside the code tags to make it more readable.

06-29-2004, 12:44 PM
If you don't want to mess around with mouse clicks, just put an invisible image on top of your images.
Just define another image element and place it absolutely on top of the original image with the same width and height. This new image should be transparent so that the original image will be shown beneath it (set its z-index value appropriately).
Now when people will copy the image to the clipboard, or even right-click it and save it, then will actually save the invisible image and not the original image.


06-29-2004, 12:55 PM
But all of that are "exercises in futility" as discussed in the link that oracleguy posted.

06-29-2004, 05:13 PM
I agree
Just wanted to show another method that doesn't involve right-clicks...