Forum Moderators: open

Message Too Old, No Replies

Disable Right Click

Disable Right Click

         

Vermont

2:19 am on Jan 24, 2004 (gmt 0)

10+ Year Member



Hi all,

I know all the negative publicity disabling right click has...be that as it may there is a good reason I want to try this:

I would like to be able to disable right clicking on only a certain part of a web page, say between two div or span tags. How would one go about this and is there some example code floating around?

Thank you in advance,
Ron

Vermont

7:34 am on Jan 24, 2004 (gmt 0)

10+ Year Member



anybody?

thaiguy

12:29 am on Jan 26, 2004 (gmt 0)

10+ Year Member



Don't think it's doable, but maybe it's possible if you use frames with invisible borders or margins and then have the disable right mouse button script operating in one of the frames only? Remember, people can just retrieve pics etc from their browser's cache, save the entire webpage to their hard drive, or use "Print Screen" to do a screen grab. Of course, you could use Javascript to eliminate the browser tool bar so there's no menu with a "Save As" option or maybe find a disable Print Screen button script as well, but then people could just turn Javascript off, anyway. :)

isitreal

9:25 pm on Jan 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's doable, I just don't see the point of it personally, but it was an interesting question, there's probably a bunch of ways to do it, this was just the first to come to mind. This one won't work on opera, but it does work. For added protection against javascript being turned off you could use document.write to write in the div you don't want people right clicking on.

<html>
<head>
<title></title>
<script language="javaScript" type="text/javascript">
<!--
// note, this script doesn't work with Opera yet.
NS4 = document.layers? 1 : 0;

function block(){return false;}

function noKey(e) {
if(NS4){
if (e.keyCode == 96){ return (false);}
} else {
if (event.keyCode == 96){ return (false);}
}
}

function disable(e){
if(NS4){
return false;// found a ns4 bug here: if(e.which > 1) {return false;}
} else {
if(event.button > 1){return false;}
}
}

function block_right_click( elementID )
{
if(NS4){
document.captureEvents(Event.MOUSEDOWN);
}
document.getElementById( elementID ).oncontextmenu = block;
document.getElementById( elementID ).onkeypress = noKey;
document.getElementById( elementID ).onmousedown = disable;
document.getElementById( elementID ).onmouseup = disable;
}
// -->
</script>
</head>

<body>
<div id="norightclick" style="height:300px;background-color:blue;" onmouseover="block_right_click('norightclick');"></div>
<div style="height:200px;background-color:green;"></div>
</body>

</html>

christodd

11:08 pm on Jan 31, 2004 (gmt 0)

10+ Year Member



Here yah go,

<script language=Javascript>
function mousedown(evt)
{
evt = (evt)? evt : ((window.event)? window.event : "")
if (evt)
{
var clickType=1;
if (navigator.appName=="Netscape") clickType=evt.which;
else clickType=evt.button;
if (clickType==1) { return true; }
}
return false;
}
</script>
<div id=noClick onMouseDown="mousedown();"></div>

isitreal

11:25 pm on Jan 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



to: christodd: That doesn't work as posted, did you forget something?