Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts

    disabling right clicks

    Hello

    I saw quite a few scripts that allow the user to disable the right-click on certain components (usually images).

    This is what the code looked like (for IE);
    Code:
    function disableRightClick() {
      if (event.button == 2) {	
        alert("Sorry, right-click is disabled");
        return false;
      }
    }
    Then call this function like so:
    Code:
    <a href="test.html" onmousedown="disableRightClick();">test</a>
    I would like to disable right-clicks without the annoying alert.
    I just don't want the menu to open when the user right-clicks the link.
    Is that possible?
    When I remove the call to alert, then the code has no effect.

    thanks

  • #2
    New Coder
    Join Date
    Nov 2003
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <a href="test.html" oncontextmenu="return false;">test</a>

    Not sure how compliant that is.

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    The reason it stops working when you remove the alert() command is that the alert box takes the focus away from the menu. I dunno if you can get it to work silently. I do have to refer you nonetheless to see the sticky on disabling right click functionality just in case you weren't aware.
    Last edited by oracleguy; 06-28-2004 at 10:22 PM.
    OracleGuy

  • #4
    New Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the help

  • #5
    New Coder
    Join Date
    May 2004
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you aware that anyone can simply select (highlight) text or images with the left button and then hit Control+c to copy it to their clipboard?

    The following works a lot better, but I'm sure some expert can find a way around it:

    <head>
    <script>
    var message="";
    ///////////////////////////////////
    function clickIE() {if (document.all) {(message);return false;}}
    function clickNS(e) {if
    (document.layers||(document.getElementById&&!document.all)) {
    if (e.which==2||e.which==3) {(message);return false;}}}
    if (document.layers)
    {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
    else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
    document.oncontextmenu=new Function("return false")
    </script>
    </head>

    <body onselectstart="return false">

  • #6
    New Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks
    FYI, it is best to place code inside the code tags to make it more readable.

  • #7
    New Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts

    here is another nice trick

    If you don't want to mess around with mouse clicks, just put an invisible image on top of your images.
    Just define another image element and place it absolutely on top of the original image with the same width and height. This new image should be transparent so that the original image will be shown beneath it (set its z-index value appropriately).
    Now when people will copy the image to the clipboard, or even right-click it and save it, then will actually save the invisible image and not the original image.

    Cheers

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    But all of that are "exercises in futility" as discussed in the link that oracleguy posted.

  • #9
    New Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I agree
    Just wanted to show another method that doesn't involve right-clicks...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •