English 中文(简体)
Opera, custom context menu, picking up the right click
原标题:

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.

问题回答

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





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签