2

I want to implement a custom context menu on a site that I am working on. I have used document.oncontextmenu which works with all, except Opera, the main browsers that I am developing for. How would I the same result in Opera? I need to disable the default context menu and display mine.

The first thing I would like to do is pick up the right mouse click, as the document.body.onmousedown is not recognized in Opera...

Thanks, R.

2
  • 2
    It's annoying but I've heard it is an ideology thing with Opera. They believe the user should always have access to the Opera context menu. If you do find a work around to disable the default context menu I'm sure there are many of us that would like to know!
    – DMKing
    Dec 9, 2009 at 15:45
  • 1
    Finally, this has been fixed in Opera 10.5 Aug 2, 2010 at 7:55

4 Answers 4

3

Doing a little research, I found an interesting little tidbit on a jQuery context menu plugin

*Opera 9.5 has an option to allow scripts to detect right-clicks, but it is disabled by default. Furthermore, Opera still doesn’t allow JavaScript to disable the browser’s default context menu which causes a usability conflict.

Found another interesting bit that may lead you in the right direction in the opera.linux google group

Opera doesn't support the javascript event oncontextmenu which these scripts use. Opera does support onrightclick, but as you see that is disabled by default. I've been told in the past by our developers that implementing support is not as trivial as making oncontextmenu an alias for onrightclick, the former apparently does a lot more.

... and this code snippet using event.button to test for right click ...

          addEventListener('mouseup',function(e){
                    if( e && e.button == 2 ){
                            document.write('a');
                            return false;
                    }
            },true); 

However, none of these solutions are going to give you what you desire (based on the first quoted segment)... Seems Opera is of the opinion that right clicking on web pages should always give the user the "standard" context menu.

1

I believe Opera deliberately fails to pass right clicks to scripts. Too many websites with silly "copy protection" pop-up messages on right click, I guess.

I assume changing (right click -> Edit Site Preferences -> Scripting -> Allow scripts to detect context menu events) makes Opera behave as you expect?

1
  • 2
    Thanks, useful bit of information but no it didn't achieve what I wanted, which was a shame :) Its not a copyright thingy its actually added functionality so the fact that it doesn't work is slightly annoying.
    – flavour404
    Dec 9, 2009 at 0:58
1

uTorrent web UI somehow show custom context menu on rightclick in opera. You can check its sources to find out how it was implemented.

0
1

In a nutshell: You have to enable the preference in Opera's settings, and oncontextmenu is not supported yet so you have to listen to the mousedown or click event.

Some scripts use a (really ugly) hack to disable Opera's own context menu when the preference is enabled: from the mousedown event, create an invisible <input type=button> element and place it where the click occurs. Because Opera has no right-click menu for buttons, its internal menu won't appear. As I said it's very ugly :-p

Proper oncontextmenu support should arrive in the next release version of Opera - not counting minor stability and security updates of course. If you don't like the ugly workaround just wait for it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.