FAQ Topic - How do I disable the right mouse button? (2009-10-18)

F

FAQ server

-----------------------------------------------------------------------
FAQ Topic - How do I disable the right mouse button?
-----------------------------------------------------------------------

The nonstandard ` contextmenu ` event is not widely supported.
Browsers that do support it may configured to prevent scripts from
accessing that event.

Example:

<body oncontextmenu="return false">

Disables the context menu where supported. Note that this will not
prevent users from viewing your source code or copying images. To
discourage copying, provide a copyright notice.

http://msdn.microsoft.com/en-us/library/ms536914(VS.85).aspx

https://developer.mozilla.org/En/DOM/Window.oncontextmenu


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/
 
S

Stevo

Is there no room in the FAQ for a flippant (Pointed Ears style) answer?

"Undo the screws at the bottom of your mouse and open it up, then cut
the wires going to the right mouse button switch." :)
 
E

Evertjan.

Stevo wrote on 18 okt 2009 in comp.lang.javascript:
Is there no room in the FAQ for a flippant (Pointed Ears style) answer?

"Undo the screws at the bottom of your mouse and open it up, then cut
the wires going to the right mouse button switch." :)

Warning:

The right button could be the wrong button to disable, as you then are left
with only the left one active, which could be the wrong one because if you
turn the mouse by the usb or ps2 cable, the right button would be left, and
with real wireless mice there are no wires to cut anyway.
 
J

Jorge

Is there no room in the FAQ for a flippant (Pointed Ears style) answer?

"Undo the screws at the bottom of your mouse and open it up, then cut
the wires going to the right mouse button switch." :)

Section 2: Which button is -exactly- the "right" one ?
 
T

The Natural Philosopher

rf said:
So then you simply cut the right wireless.
no, you cut the PCB track. Or wedge a matchsick under the button.

Or give them a one button macinstosh mouse.
 
V

VK

Is there no room in the FAQ for a flippant (Pointed Ears style) answer?
"Undo the screws at the bottom of your mouse and open it up, then cut
the wires going to the right mouse button switch." :)

Section 2: Which button is -exactly- the "right" one ?

"the context menu invoking button, the rightmost by default on the
majority of mouse models" might be more accurate taking into account
that the button functions can be easily swapped. The
eventObject.button property is attached exactly to the current buttons
*functionality*, not to their physical allocation left-to-right.

Overall the FAQ topic is written "with a hate in it", so more
accentuated on "don't do it!" rather than on "how to do it?". It has
some historical logic behind it, because at the end of the last
century, when the FAQ were made, one of most annoying questions were
"How to prevent my page copied?" and "How to prevent my pictures
saved?" etc. with 90% of context menu questions posed with it in mind.

Now with the new stage of development of Web applications custom
context menus support is a obvious must to any market-oriented
browser, so respectively all of them support oncontextmenu handler.

The main troublemaker is IE as usual. The first caveat is the shorten
bubble retention period on IE (a prehistoric stability connected
restriction no one bothered to remove ever since). This way
<body oncontextmenu="return false;">
will work on IE just fine (context menu locked) while
<body oncontextmenu="return someFalseReturningFunction();">
will be ignored by IE as the system will not hold the bubble for
someFalseReturningFunction
The second caveat is that while others (Gecko, Safari, Chrome) apply
the handler to the window, IE applies it to document.body only and
document.body doesn't necessary apply to the whole viewport.
To overcome all this an universal solution could be like:

<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type"
content="text/html; charset=windows-1252">
<style type="text/css">
HTML {
width: 100%;
height: 100%;
}
BODY {
width: 100%;
height: 100%;
}
</style>
<script type="text/javascript">

function contextMenu(e) {
if (e.button == 2) {
// do context menu
window.alert('Custom context menu');
return false;
}
else {
return true;
}
}
</script>
</head>
<body
oncontextmenu="return false;"
onmouseup="return contextMenu(arguments[0] || event);">
<p>click anywhere in this page</p>
</body>
</html>
 
G

Garrett Smith

rf said:
So then you simply cut the right wireless.

What about for trackpad or joystick? What about touch screen devices?

What if the user activates a context menu by other means (shift +f10 in
Opera).

The presence of a mouse cannot be detected and should never be assumed.

(and body modification should be mentioned in the FAQ)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top