Disable rightclick menu in Chrome?

Hi all,

I am working on an existing page for a client, he wants right click disabled in Chrome and Safari (dont ask!). The existing Javascript code works to disable the rightclick / menu in IE and FF, but not Chrome and Safari.

Could anyone tell me how, if at all posible can I disable the right click menu in Chrome? Any help MUCH appreciated, im on a deadline to fix this.

Kind regards and thankyou

Existing Code (not my code):


	<!-- Copyright Alert Function -->
		function click(e){
			var message="You cannot do that";
			if (document.all){ // for IE
				alert("IE")
				if (event.button==2||event.button==3){
				alert(message);
				return false;
				}
			} else { // for Firefox
				if (e.button==2||e.button==3){
					alert("FF")
					e.preventDefault();
					e.stopPropagation();
					alert(message);
					return false;
				} else {
					alert("Chrome / Safari");
					}
				}
			}
		}
		if (document.all){ // for IE
			document.onmousedown = click;
		} else { //for FF
			document.onclick = click;
		}

I have to head off, but briefly, have you considered performing some research with a google search? You can nicely narrow your options by using the right keywords.

“google chrome” javascript disable right-click

Since I’m also limited with time, just a suggestion - do it with jQuery. You’ll save time.

Perhaps those browsers default to disabling the ability of web pages to do that. Most modern browsers at least allow the browser owner to disallow web pages ability to interfere with the operation of their operating system in that way.

Here is a way to examine all mouse click events. It works in all browsers, both IE and non-IE. The only problem I found was the wheel click event in Opera - it doesn’t show the button integer. Try right, middle and left clicking on the page in all of your browsers.

I tested this in Chrome 8.0.552.237, Firefox 3.6.13, Opera Ver:11, Safari 5.0.3 and IE 5.5, 6, 7, and 8.

[HIGHLIGHT=“”]
<!doctype HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=windows-1252”>
<title>Mouse click events</title>
<script type=“text/javascript”>
<!–
// examines browser mouse click events
function click(evt)
{ // rationalise event syntax
var e=(evt)?evt :window.event;
var message=“You cannot do that”;
// test for IE
if(typeof e.which==“undefined”)
{ // right click event
if(e.button==2)
{ alert(message+" IE= "+e.button);
return false;
// ---------
}
else
{ // other types of events
alert("IE= “+e.button);
}
}
else
{ // for other browsers
if(e.which==3)
{ alert(message+” not IE= "+e.which);
e.preventDefault();
e.stopPropagation();
return false;
// -----------
}
else
{ // other types of events
alert("not IE= "+e.which);
}
}
}
// ----------
function init()
{ document.onmousedown=click;
document.oncontextmenu=new Function(“return false”)
}
// -----------
window.onload=function (){ document.onmousedown=click; document.oncontextmenu=new Function(“return false”); }

//–>
</script>
</head>

<body onload=“init()”>

<div id=“test”>
Click on page to see event alert</div>
<!-- end test div –>

</body>

</html>

Thankyou very much all for your kind help, and great script Allan thanks :slight_smile:

Except it can’t be used to block the right mouse button when the browser is set to disallow blocking the right mouse button - when the browser is set to disallow blocking that the normal right mouse button functioning occurs before the click is passed to the web page and may not even be passed to the web page depending on the browser.

Anyway that code does not block the context menu being accessed from the menu key on the keyboard (to the left of the right CTRL key) and so cannot prevent access to the menu even if the browser does allow the mouse button to be blocked as all the person would need to do to get around that their right mouse button is blocked is to give the desired field the focus (tabbing to it using the keyboard is the easiest way) and then hit the menu button.

Alternatively if their browser doesn’t allow them to override blocking that mouse button and they don’t want to switch to a browser that does they can easily add a bookmark script that they can click on to restore the normal functioning of the right mouse button if the page blocked it. eg.http://javascript.about.com/library/blright.htm

Of course another option they could use is to simply turn JavaScript off for the web page. JavaScript is a web page is only useful when it makes using the page easier for the person visiting and blocking the functioning of their browser does not do that so they will obviously just turn it off one way or another.

The only people you can block from using their right mouse button are newbies who haven’t yet discovered how easily such blocks can be overcome.

I think most of us realise that you can’t really block people who are intent upon copying the page content. However, it does slow down the honest people and, given the number of beginners asking how to do this, it is still somewhat of a mystery to many people. In summary, worth doing, but offering low security.

Well, I certainly wouldnt do it myself… but you cant argue with a client, even if you give a firm recomendation against it :slight_smile:

You really should argue with the client on that one - after all you are the professional and they will lose a lot of legitimate visitors if they insist on trying to do that without it actually doing anything with respect to the reason why they want it there. If anything adding a script to try to block the right mouse button will increase the number of people stealing the page content just because they think it is somehow clever that they know how to bypass that block.

Ask them if that is what they really want to achieve as that is all that blocking the right mouse button does.

If they really do want to encourage people to steal their content and to drive away legitimate visitors then you should proceed to implement what they have asked for as then the results of doing so will match their expectations.

If they don’t want to drive away legitimate visitors and increase the amount of content theft then you need to ask them why they have asked you to implement something that does that instead of implementing something that achieves what they actually want.