Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How do you disable the contextmenu with DOM event handlers

  1. #1
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default How do you disable the contextmenu with DOM event handlers

    Hello,

    So How do you disable the contextmenu with DOM event handlers?

    I used to use

    Code:
    document.oncontextmenu=function(){return false}; 
    document.oncontextmenu='';
    But what do I use with, for example

    Code:
    if(document.addEventListener)
    			{
    			document.addEventListener('contextmenu', returnFalse, false); 
    			}
    function returnFalse()
    	{
    	return false;	
    	}
    because it doesn't seem to work.

    Thanks in advance


    EDIT: And how do you enable the menu after you've disabled it?


  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    if(document.addEventListener)
      document.addEventListener('contextmenu', function(e) { e.stopPropagation(); });
    else if(document.attachEvent)
      document.attachEvent('oncontextmenu', function() { event.cancelBubble = true; });
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    blummin' eck that was fast!

    OK thats fantastic, but how do I re-enable the menu?


  4. #4
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    I can't get it to work.

    Firebug report 'not enough arguments' and the menu still displays

    Do you have any idea as the rest of the web seems short on them.


  5. #5
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Sorted

    Just needed to add the argument false to the addeventlistener to stop bubbling.
    Code:
    if(document.addEventListener)
      document.addEventListener('contextmenu', function(e) { e.stopPropagation(); }, false);
    else if(document.attachEvent)
      document.attachEvent('oncontextmenu', function() { event.cancelBubble = true; });

    I've sorted the removeEventListener by creating my own function to be called and removing that.

    Code:
    function disableContextMenu(e)
    	{
    	if(!e)
    		{
    		event.cancelBubble = true;
    		}
    	else
    		{
    		if(e.stopPropagation)
    			{
    			e.stopPropagation();
    			}
    		else
    			{
    			return false; //redundant as not called	
    			}
    		}
    		
    	}
    Last edited by Bob90; 05-08-2007 at 11:47 AM. Reason: Added extra info

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Bob90 View Post
    Just needed to add the argument false to the addeventlistener to stop bubbling.
    The third argument to the addEventListener and removeEventListener methods doesn't affect bubbling. It determines in which event phase the listener to be added or removed would be called. Passing true places the listener in the capturing phase, whilst passing false places the listener in the bubbling phase. In general, capturing should never be necessary.

    To prevent bubbling, call the stopPropagation method, or assign true to the cancelBubble property.

    To disable the default action for a particular event, call the preventDefault method, assign false to the returnValue property, or return false. The latter option is preferable to the second as they are equivalent, but the latter is compatible with more browsers.

    Mike

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    None of this stuff (it is essentially 'no right click' dressed up in drag) should be relied upon for anything. It will work in many cases and it will not work in may others. Even (if the issue is copying things) where it 'works' users will find another way to get the information they want copied.

    Something like this is only appropriate where it enhances the web experience for the user. If it is for any kind of 'protection', get a condom.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    I feel I need to defend myself from the heresy that is 'Javascript Protection'.

    I never intended it to be for protection, I hope that all regular coders would know javascript is a placebo. (In the sense that it may be called protection, but offers very little except self satisfaction for the newbie)

    I am creating a radial menu for my site, which I want to code properly. At the moment it is a mess and I merely wanted a way to stop the menu appearing.

    p.s. the condom joke is a winner.


  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Sounds like an excellent use for this sort of code, except if you are trying to replace the context menu with your radial menu, that would be problematical as, many users would never get to see it, and others would see it overlaid by the default context menu for their browser.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    Thanks for your concern, but as you can gather from my second post I only want to temporarily disable it.

    As for the people with problems, I am working around this at the moment.

    If you have any suggestions I would love to hear them (About working around the contextmenu).


Bookmarks

Posting Permissions

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