FAQ Topic - How do I disable the right mouse button? (2010-08-13)

Discussion in 'Javascript' started by FAQ server, Aug 13, 2010.

  1. FAQ server

    FAQ server Guest

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

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


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

    <URL: http://msdn.microsoft.com/en-us/library/ms536914(VS.85).aspx>
    <URL: https://developer.mozilla.org/En/DOM/Window.oncontextmenu>

    The complete comp.lang.javascript FAQ is at
    FAQ server, Aug 13, 2010
    1. Advertisements

  2. FAQ server

    Evertjan. Guest

    FAQ server wrote on 13 aug 2010 in comp.lang.javascript:
    What nonsense.

    Copying [as in pressing ctrl-C] does not infringe copyright.

    Copying [part of] a webpage has as much to do with copyright,
    as [the act of] giving birth has to do with birthright.
    Evertjan., Aug 13, 2010
    1. Advertisements

  3. What nonsense?
    Did you misconstrue?

    Is the entry written ambiguously? I suppose "unauthorized reproduction"
    is better than "copying".
    Garrett Smith, Aug 13, 2010
  4. FAQ server

    David Mark Guest

    How about the title? How many times do you have to be told that it is
    not always the "right" mouse button that initiates a context menu. In
    fact, some mice have but one button.
    Doesn't appear so. The comment about copyright notices is silly too.
    David Mark, Aug 14, 2010
  5. So what? It's still a frequently asked question.
    Not really.
    You can't technically prevent copying.
    You can't morally prevent copying that is lawful.
    So, the only copying that you can do anything about is unlawful
    copying, which is what the copyright notice is there to discourage.

    Lasse Reichstein Nielsen, Aug 14, 2010
  6. FAQ server

    David Mark Guest

    There should be some explanation in the FAQ about the many ways in
    which it is the wrong question. There may be no right mouse button,
    there are other ways to invoke a context menu, there are ways besides
    the context menu to copy content and view the source, etc.

    And this leads to many other similar questions which are not featured
    in the FAQ. Best to come up with a better title that encompasses more
    than just the "right mouse button" question.
    It is too! :)
    It's a stretch for a question in a JS FAQ titled "How do I disable the
    right mouse button".
    David Mark, Aug 14, 2010
  7. The title is not what Evertjan was commenting on.

    The reason it says "right button" is that is the common thing that the
    green developer will think in terms of.

    Sure, a context menu can be triggered by control-click on mac or
    specially configured mouse; but that does not mean the title of the
    entry needs changing.
    Garrett Smith, Aug 14, 2010
  8. FAQ server

    David Mark Guest

    Did I say it was? Or did you read that into it in your own inimitable
    Among a dozen other things related to "protecting" their source code.
    You need changing.
    David Mark, Aug 14, 2010
  9. FAQ server

    Evertjan. Guest

    Garrett Smith wrote on 13 aug 2010 in comp.lang.javascript:
    There is no authorisation necessary for copying what is on the web.

    Only the publishing could be unlawful without authorisation,
    and then only if it is a work of art or craft, like a programma,
    data are never under copyright,
    nor is the manner of a programme's invocation.

    So if you don't want your work to be copied,
    there is only one simple and effective method:

    Do not publish it.
    Evertjan., Aug 14, 2010
  10. FAQ server

    Evertjan. Guest

    Garrett Smith wrote on 14 aug 2010 in comp.lang.javascript:

    The nonsense is that "copyright" on the web is NOT about copying,
    as all webpages are copied [ to the cashe ] to be seen.

    The Dutch word for "copyright" is "auteursrecht",
    a better word, since this is about the rights of the autor.

    Preventing copying to the clipboard has nothing to do with preventing
    copyright infringement.

    Indeed, also the "right button" could be the wrong button,
    if buttons are swapped, say by a lefthanded mouse user.
    Indeed. one should comment on what one quotes.

    If one comments on the subject, repeat the subject as a quote,
    or at least use explicit reference to such subject line.
    Evertjan., Aug 14, 2010
  11. In comp.lang.javascript message <4c647cfa$0$274$
    Right Click is supported, using XP sp3, in IE8, Firefox 3.0.19, Opera
    10.10, Safari 5.0, Chrome 5.0. That seems reasonably wide to me, even
    if no other browser and no non-MS operating system support it, which
    seems unlikely.
    Making a setting on a particular page is not configuring the browser.

    Something more like "For some/many/most browsers, including in the page
    '<body oncontextmenu="return false">' will inhibit any response to
    clicking the right mouse button on the body element." would be better.

    It will prevent those users who believe it to be the only way.
    Dr J R Stockton, Aug 14, 2010
  12. FAQ server

    David Mark Guest

    "Right Click"?!
    You missed the point, doc. You can configure some browsers (e.g.
    Opera) to ignore such handlers/listeners.
    I don't know that it would be better, but it is certainly a lousy
    suggestion (for numerous reasons).

    For one, virtually all browsers render the HTML element (exception is
    IE in quirks mode and IE < 6). Imagine if the body has margins.

    For two, configuring the OS mouse settings for left-handed users will
    swap the left and right mouse button behaviors. Furthermore, the
    context menu can often be triggered with a keystroke. So the mention
    of "clicking the right mouse button" is not a very good description.

    Obviously, the question that the typical neophyte wants answered is
    how to disable the context menu (which is still a bad question, but at
    least closer to what they have in mind).
    Not really (see point #1).
    David Mark, Aug 15, 2010
  13. FAQ server

    amin arab Guest

    this not good way for this work in IE and Opera and chrome . if width
    of body not equal to 100% then some browser can cancel only part pf
    that putted element and so i offer this code :

    <script type="text/javascript" >
    function mischandler(){
    return false;

    function mousehandler(e){
    //var myevent = (isNS) ? e : event;
    var myevent = e || event;
    //var eventbutton = (isNS) ? myevent.which : myevent.button;
    var eventbutton = myevent.which || myevent.button;
    if((eventbutton==2)||(eventbutton==3)) return false;

    document.oncontextmenu = mischandler;
    document.onmousedown = mousehandler;
    document.onmouseup = mousehandler;

    http://www.computerhope.com/j20.htm (with little change to have not
    dependency to check browser).
    test it on other OS . this maybe will work.

    amin arab, Aug 15, 2010
  14. Indeed support has been growing. But it cannot be relied on. Support may
    depend on the OS and touch screen devices cannot be expected to support it.

    How about changing "widely" to "universally"?

    Seems sufficient to me. How a browser is configured depends on the
    browser; there isn't one way.

    For example, in Firefox (with English settings), go to "Tools > Options
    dialog will appear with the heading "Allow scripts to:". Uncheck
    "Disable or replace context menus".
    So the example follow its preceding paragraph might present the possible
    interpretation that the example demonstrates what is explained (about
    configuring the browser). Did I get that right?
    An explanatory paragraph is a good idea.

    Replace "some/many/most" with "where supported" (because that wording
    includes browsers that support the event but have been configured to
    disallow it).

    Focusing on "right button" in the subject is ok, but the answer should
    not pretend that that is the only wayt to rigger a context menu.
    Describing that the code inhibits response to right click might is not
    misleading. Instead, the answer should use "suppress the context menu"
    instead of in place and perhaps change "inhibit" to "suppress" and
    replace "any response" to "the context menu". Even when the right click
    is the context menu click, then it still triggers a series of events.
    Thus, it does not inhibit any response (it suppresses the context menu).

    An alternative:

    "Where supported, `<body oncontextmenu="return false">` will suppress
    the the default context menu on the `BODY` element.

    If the terminology is unfamiliar to an inexperienced beginner, then he
    is at least left with a line of code that he can quickly try out.
    I don't have statistical data on the
    users-who-believe-right-click-is-the-only-way-to-view-source group (much
    less demographics of that ilk), but I suspect it's pretty dang low.
    Users tend to click on the application menu items when they want to do
    something and don't know how.
    Garrett Smith, Aug 15, 2010
  15. That's still invalid HTML. Why didn't I notice earlier? And so if the
    solution is invalid HTML, then perhaps it would be better to use an
    event handler property example.

    function returnFalse() {
    return false;

    document.documentElement.oncontextmenu = returnFalse;

    I've not tested it. The approach can't be relied on.
    Garrett Smith, Aug 15, 2010
  16. In comp.lang.javascript message <[email protected]
    0g2000pro.googlegroups.com>, Fri, 13 Aug 2010 18:14:42, David Mark
    But the people who actually ask the question do not know that. It is
    important that FAQ section Subjects, whether or not they match the
    wording of the Subject of a particular article, should be meaningful to
    those who are looking for answers.

    And it should be obvious that, because disabling the context menus is of
    limited use, the answer to the question will be sought by those of
    limited knowledge.
    Dr J R Stockton, Aug 15, 2010
  17. Sorry that didn't sense.

    What I meant to say was the answer should use "suppress the context
    menu" instead of "inhibit any response".
    That's no good because it's invalid HTML.

    | Where supported, `document.oncontextmenu = function(){return false};`
    | will suppress the default context menu on the document.
    Garrett Smith, Aug 15, 2010
  18. In comp.lang.javascript message <[email protected]
    g2000prg.googlegroups.com>, Sat, 14 Aug 2010 16:18:35, David Mark
    I tested with a Right Click, so I reported it as such without making
    assumptions about what anything else does. "Right Click" means to use
    the (current) right mouse button, and it is that which the Subject
    rightly refers to.

    Think (again, if possible) : only a small proportion of Web page users
    know about that, and a smaller proportion do it.

    Advice in the FAQ should reflect what generally works, with appropriate
    warnings if it does not always work.

    For example, the expression NaN + NaN does not always give NaN .

    By convention, the right button (the one referred to in the Subject
    line) is then on the left.

    The Subject question concerns the right mouse button.

    No, because the typical neophyte does not know that it is called
    "context menu".

    If they try what they believe to be the only way, and it fails to work,
    they will of necessity give up. Remember, it is the neophyte thinking
    that matters, not yours.

    It is a great pity that you can type faster than you think.
    Dr J R Stockton, Aug 16, 2010
  19. In comp.lang.javascript message <i49d2v$t9g$-
    september.org>, Sun, 15 Aug 2010 11:53:17, Garrett Smith
    No. Your "where supported" is strictly accurate; but unhelpful; it
    carries, for many readers, an implication that support is uncommon.
    ISTM that support is general, but not necessarily universal; the FAQ
    wording should reflect that.

    It's not a question that I recall being asked very often here.
    Dr J R Stockton, Aug 16, 2010
  20. It shouldn't; one should, however, be aware that the feature cannot be
    expected to work.
    Try disabling the context menu as per the explanation of how to do that
    in Firefox. Such capability cannot be expected.

    For Internet Explorer, the administrator may disable the context menu by
    using a Registry Entry:
    | NoBrowserContextMenu:
    | Disables the right-click shortcut menu on a Web page.

    If the user wants to use right click, he will. He will probably expect
    that when he does that, he gets the browser context menu (with the
    things like "back", "view source", "open link in new tab" or any number
    of other things that he has learned about what should appear in the menu
    when he right clicks.

    The user will not expect anything new from any site in general (nothing
    beyond what his browser provides in that menu) and so if you want to
    change the user's expectation by suppressing the context menu and
    providing your own (based on a UL or DIV) -- and you can do that in some
    browsers -- you'll need to provide strong clues and indicators to the
    user. Otherwise, the user will apply his expectations or preconceptions
    (or whatever you want to call it) of what right-click is about. Such
    clues might include instructions and heuristics. Again, it is possible
    -- and, for more desktop-like webapp, especially a closed intranet app
    -- not unthinkable.

    Even you do that, and you suppress the context menu and provide your own
    menu, then it can still fail in some environments. Again, what about
    touch-screen devices? No right-click there.
    Right-click scripts were common and more popular around 2002 or so
    (dynamicdrive.com had such inadvisable things).
    Garrett Smith, Aug 17, 2010
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.