最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Opera, custom context menu, picking up the right click - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share edited Dec 9, 2009 at 1:02 random 9,95610 gold badges69 silver badges84 bronze badges asked Dec 9, 2009 at 0:34 flavour404flavour404 6,31230 gold badges106 silver badges140 bronze badges 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 Commented Dec 9, 2009 at 15:45
  • 1 Finally, this has been fixed in Opera 10.5 – user123444555621 Commented Aug 2, 2010 at 7:55
Add a ment  | 

4 Answers 4

Reset to default 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.

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?

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

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

发布评论

评论列表(0)

  1. 暂无评论