...

View Full Version : disabling right clicks



davidklonski
06-28-2004, 10:47 PM
Hello

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.

thanks

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

Not sure how compliant that is.

oracleguy
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.

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

starrwriter
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:

<head>
<script>
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.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function("return false")
</script>
</head>

<body onselectstart="return false">

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

davidklonski
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.

Cheers

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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum