Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a close event for the browser contextmenu

I'm catching the contextmenu event using jQuery like this:

$(document.body).on("contextmenu", function(e){
    //do stuff here
});

So far, so good. Now I want to execute some code when it closes but I can't seem to find a correct solution for this.

Using something like the following would catch some of the cases, but not nearly all:

$(document.body).on("contextmenu click", function(e){});

It wouldn't be executed when:

  • the browser loses focus
  • an option in the contextmenu is chosen
  • the user clicks anywhere in the browser that's not on the page

note: I'm not using a jQuery context menu, I'm just using it to catch the event.

like image 374
red-X Avatar asked Oct 09 '12 14:10

red-X


People also ask

What is ContextMenu event?

The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse button, or by pressing the context menu key.

How do I close the context menu?

ContextMenu can be opened and closed programmatically whenever required by using open and close methods. In the following example, the ContextMenu is opened using the open method at the specified position using top and left . Also, ContextMenu is closed using close method on ContextMenu item click or document click.

How do you create a context menu in HTML?

The contextmenu global attribute is the id of a <menu> to use as the contextual menu for this element. A context menu is a menu that appears upon user interaction, such as a right-click. HTML now allows us to customize this menu. Here are some implementation examples, including nested menus.

Which actions displays the Windows context menu?

In Microsoft Windows, pressing the Application key or Shift+F10 opens a context menu for the region that has focus.


1 Answers

Following code may help you. jsfiddle

var isIntextMenuOpen ;
$(document).on("contextmenu", function(e){

    isIntextMenuOpen = true;
});
function hideContextmenu(e){
       if(isIntextMenuOpen ){
            console.log("contextmenu closed ");
       }

     isIntextMenuOpen = false;
}
$(window).blur(hideContextmenu);

$(document).click(hideContextmenu);
like image 95
Anoop Avatar answered Sep 30 '22 17:09

Anoop