Controlling right-click using JavaScript
We've all seen web sites that try dismally to protect their content by disabling the right-click driven context menu appearing. However, you might have a use for using the right-click as a prompt to do something useful. If you do, here's a quick solution:
<html>
<body>
.....
<script>
function init() {
var div = document.getElementById('myChoosenDiv');
div.onmousedown =
function (e) {
var rightclick;
if (!e) var e = window.event;
if (e.which) rightclick = (e.which == 3);
else if (e.button) rightclick = (e.button == 2);
if (rightclick) doSomething();
else doSomethingElse();
return false;
};
}
</script>
<body oncontextmenu="return false" onload="init()">
<div id="myChoosenDiv">
Right click to do something or left
click to do something else
</div>
...
</body>
</html>
Seems to work in IE7 and FF2 on Windows XP
0 TrackBacks
Listed below are links to blogs that reference this entry: Controlling right-click using JavaScript.
TrackBack URL for this entry: http://www.robbiebow.co.uk/mt/mt-tb.cgi/33

Leave a comment