jQuery and ContextMenu plugin - right click in Opera

    Recently, I faced a task - to implement in the admin panel of one project a context menu by right-clicking. To solve this problem, I found a plug-in for jQuery - contextMenu , a demonstration . I looked at the example, downloaded, screwed it and began to check. IE - normal, FF - normal, Safari - normal, Opera - not working!

    And so, the problem:
    Opera does not allow you to create your own context menu and track the right mouse click.

    Decision:
    As you know (or don’t know), the Opera has JavaScript settings (Tools - Settings - Advanced - Content - JavaScript Settings), there is the property “Allow to control the right mouse button”. By clicking on it we have the result - JS catches the right-click event and shows the context menu, but the standard menu falls on top of it. A little googling found a solution to his problem:

    1.     // Проверяем браузер, и если это Опера выполняем скрипт
    2.     if(navigator.appName.indexOf("Opera")!=-1){
    3.       // Инициализация скрипта
    4.       (function(){
    5.         // Скрываем контекстное меню, когда нажата кнопка мыши
    6.         addEventListener('mousedown',function(e){
    7.           // Если нажата правая кнопка мыши - меню не закрываем
    8.           if( e && e.button == 2 ){
    9.             cancelMenu(e);
    10.             return false;
    11.           }
    12.         },true);
    13.  
    14.         var overrideButton;
    15.         function cancelMenu(e){
    16.           if(!overrideButton){
    17.             var doc = e.target.ownerDocument;
    18.             overrideButton = doc.createElement('input');
    19.             overrideButton.type='button';
    20.             (doc.body||doc.documentElement).appendChild(overrideButton);
    21.           }
    22.           overrideButton.style='position:absolute;top:'+(e.clientY-2)+'px;left:'+
    23.           (e.clientX-2)+'px;width:5px;height:5px;opacity:0.01';
    24.         }
    25.     
    26.       })( true, 1000 );
    27.     }
    * This source code was highlighted with Source Code Highlighter.


    The script creates a button on the page that hides the standard Opera context menu. Having screwed this script to the admin panel, I got the following (see Opera). Now I rejoice and use it.

    PS In order to avoid unnecessary questions, the script is used only in the admin panel, and therefore it was considered not scary to ask the user to enable right-click tracking.


    Also popular now: