views:

339

answers:

4

Hi,

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.

+1  A: 

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?

Baffe Boyois
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
A: 

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

Denis Bredikhin
Thanks, I will check it out.
flavour404
+2  A: 

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.

gnarf
A: 

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

hallvors