Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Ontario, Canada
    Posts
    138
    Rep Power
    32

    Disable right click contextmenu in IE (including IE7)


    I am trying to disable the right click menu from poping up when a user right clicks in IE 7. I would like the menu to not popup when right clicking on the document, or links.

    This is for a web app, and our users have requested a custom right click menu. The custom menu, surprisingly, was the easy part. I can not seem to get the default right-click menu in IE7 to go way. Currently, both menu's show up.

    I have tried createing a function that returns false that fires onContextMenu, but this hasn't worked for me in IE 7

    Does anyone have a script that may do what I am looking for, or even point me in the right direction.

    Thanks,
    M.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Location
    Brisbane, Australia
    Posts
    1,442
    Rep Power
    29
    having the function return false disables the context menu.

    If that isn't working chances are that your script is crashing or exiting before it gets to the return false bit.
    Like the answers I give? Why not ask me directly at my forum. I'm always glad to help.

    Javascript scripts and tips can be found at Dynamic Tools.
    Check out DynamicTable, the best javascript table sorter around.
    Get reliable and affordable hosting at www.thinksmarthosting.com
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Ontario, Canada
    Posts
    138
    Rep Power
    32
    Originally Posted by ProggerPete
    having the function return false disables the context menu.

    If that isn't working chances are that your script is crashing or exiting before it gets to the return false bit.
    Here is a very simplified sample of what I have:
    Code:
    <script language="javascript">
    function customRightMenu(e) {
      if (e.button==2) {
        //code for my custom right click menu
      }
    }
    function noContextMenu() {
      return false;
    }
    </script>
    Code:
    <BODY onContextMenu="noContextMenu()" >
    
    <script language="javascript">
      document.onMouseDown = customRightMenu(event);
    </script>
    The above is esentialy what I have, but right now both menus are popping up. Is there a problem with the way I am returning false to the onContextMenu?

    Your help is greatly appreciated.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Ontario, Canada
    Posts
    138
    Rep Power
    32
    *bump*

    Please, if anyone can tell me why the above code does not work (the right click menu is still poping up) or point me in the direction of a better disable right click script for IE7, I would greatly appreciate it.

    Thanks,
    M
  8. #5
  9. Resident DJ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    283
    Rep Power
    14
    It also quite possibly has been disabled by IE7 itself. Which might be the only good thing to come out of IE7.

    Disabling right-click is generally a bad idea. There is not one good reason why you should disable it. At least I cannot think of one.

    For example: If I want to save your picture to my computer, I'm going to get it whether you like it or not. In fact, in my younger years, I used to send snarky emails to the page owner who would disable right-clicking for "copyright purposes" and include their pictures as proof that their effort was futile and that if they didn't want anyone to have it, they shouldn't have shared it. But again, that was in my younger years.

    Mis-use of JavaScript like this is one (of many) reasons why it becomes generally despised: You're taking away the basic functionality of the web browser.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Ontario, Canada
    Posts
    138
    Rep Power
    32
    I agree mostly with everything you said. The reason I am disabling right click is not to stop users from taking images or code or anything like that.

    Basically, the way our web app does session handeling is causeing users to lose slight functionality. IE: when a user right clicks a link and selects 'open in a new window', the link will open in the same window.

    We have found away around this by creating a custom right click menu for our users with the 'open in a new window' option. This custom menu, and therefore, the disabling of the right click menu, is only for one page in the web app, and was requested by our users.

    I am not arguing that it is right to use, I just find myself in a position where I need to do it. So anyone who has gotten right click to disable in IE7, your help would awsome.

    Thanks,
    M.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Location
    Brisbane, Australia
    Posts
    1,442
    Rep Power
    29
    Ajax : I disagree, disabling the right click menu can often be a good idea. Doing so in an attempt to protect pictures is of course pointless.

    Disabling the context menu so I can replace you can replace it with one of your own can be a great way of providing functionality to the user.

    To brand any attempt at disabling the right click menu as misuse is stupid.

    M-Bone : The app that I have replaces the context menu and works in IE7. Try catching the event in a javascript section instead of using inline javascript.

    e.g.
    Code:
    <script>
    function mycontextmenu()
    {
    	return false;
    }
    
    document.oncontextmenu = mycontextmenu;
    </script>

    Comments on this post

    • Annie79 agrees
    • M-Bone agrees : thanks!
    Like the answers I give? Why not ask me directly at my forum. I'm always glad to help.

    Javascript scripts and tips can be found at Dynamic Tools.
    Check out DynamicTable, the best javascript table sorter around.
    Get reliable and affordable hosting at www.thinksmarthosting.com
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Ontario, Canada
    Posts
    138
    Rep Power
    32
    Hey ProggerPete,

    Thanks for defending the idea of a custom right click menu. I tried your suggestion, but it still doesn't seem to work.

    I even tried a very simple page:
    Code:
    <html>
    <head></head>
    <body>
    
    <script language='Javascript'>
       function mycontextmenu() {
         return false;
       }
      document.oncontextmenu = mycontextmenu;
    </script>
    
    </body>
    </html>
    I am testing this on my desktop, but that shouldn't affect this not working, right?
  16. #9
  17. CSKA Sofia
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Germany / Bulgaria
    Posts
    575
    Rep Power
    123
    I have just tried out you code with firebug's console and it is working fine. I've also tried it out with anonymous function -> it is also working as expected:
    javascript Code:
     document.oncontextmenu = function() { alert('hello'); return false; };


    What OS and browser are you using?
    Nikola Ivanov
    http://enikola.de
  18. #10
  19. Resident DJ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    283
    Rep Power
    14
    Originally Posted by ProggerPete
    Ajax : I disagree, disabling the right click menu can often be a good idea. Doing so in an attempt to protect pictures is of course pointless.
    Good point, one that I, as I said, could not think of. Every time I've seen it disabled has been for the most banal of reasons. I have not seen any application in which someone writes their own menu - which now that I think about it does have its advantages.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Ontario, Canada
    Posts
    138
    Rep Power
    32
    Originally Posted by execute
    I have just tried out you code with firebug's console and it is working fine. I've also tried it out with anonymous function -> it is also working as expected:
    javascript Code:
     document.oncontextmenu = function() { alert('hello'); return false; };


    What OS and browser are you using?
    Yes, just verified. The code I posted does work in firefox. It does not seem to work in IE7 (win XP). Unfortunatly, 95% of our users are IE6+

    Any ideas why it's not?
  22. #12
  23. CSKA Sofia
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Germany / Bulgaria
    Posts
    575
    Rep Power
    123
    Sometimes things are so obvious that you can not see them. You must put <script> in the head not in the body. Tested with IE7. Sorry, I don't have access to my IE6 right now.
    javascript Code:
    <html>
    <head>
    <script language='Javascript'>
       function mycontextmenu() {
    		alert('ole');
    		return false;
       }
      document.oncontextmenu = mycontextmenu;
    </script>
    </head>
    <body>
    <div id="wrapper">document</div>
    </body>
    </html>


    [edit]Tested also with IE6 - working. The Event contextmenu is however not implemented in Opera9 and therefor it is not working[/edit]
    Last edited by execute; February 6th, 2007 at 05:13 AM.
    Nikola Ivanov
    http://enikola.de
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Post I found fix.Idea - create absolute "select" element on mousedown event.


    [highlight]if(js.isIE) {
    document.oncontextmenu = function(ev){
    if(ev.button == 2){
    js.cancelEvent(ev);
    $logger.log("cancel context menu");
    }
    };

    if(js.isIE8) {
    var lSelect = document.createElement("select");
    var option = document.createElement("option");
    option.innerHTML = "hello";
    js.addClass(lSelect,"ie8ContextMenuFixEl");
    lSelect.appendChild(option);
    document.body.appendChild(lSelect);
    //hide select element
    lSelect.style.left = "-999px";
    lSelect.style.top = "-999px";

    js.attach(document,"mousedown",function(ev){
    if(ev.button == 2){
    lSelect.style.left = (ev.clientX - 10)+ "px";
    lSelect.style.top = (ev.clientY - 10) + "px";
    lSelect.focus();

    $logger.log("cancel context menu");

    //hide select element
    window.setTimeout(function(){
    lSelect.style.left = "-999px";
    lSelect.style.top = "-999px";
    },1000);
    }
    });
    }
    }
    else {
    js.attach(document.body,"contextmenu",function(ev){
    if(ev.button == 2){
    js.cancelEvent(ev);
    $logger.log("cancel context menu");
    }
    });
    }
  26. #14
  27. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    you're 5 years late. Actually, you're 20 years late, because those stupid click blockers are dead since the 90s. Most people today have realized that they're completely useless and massively reduce usability. If you don't want visitors to use your content, don't make a website.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    maybe)),but I really need block browser context menu(and use own) in ie8 and another way I not found.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo