It's a fairly common pattern to have a dialog, a menu, or some other HTML element with a link inside it pop up in response to a click. For example, if you click the orange question mark above the Stack Overflow edit box, it adds a bunch of links for "Links", "Images", etc. Then, when you click the question mark again, the menu goes away.
I have a similar setup, only I want the menu to go away when the user clicks anywhere except the menu. This isn't hard ($(document.body).click(handleClickElsewhere)). However, what is hard is the following case:
- The user clicks to bring up the menu
- The user right-clicks on one of the links in the menu (making the browser context menu appear)
- The user left-clicks on the browser's context menu, on the "Open in new tab" menu item
- The user is taken to the link in a new tab, BUT
- The menu stays open back on the original page
The hard part about the above is that I would like to instead close the menu in that case; it looks kind of silly when the user returns to their original tab, and the menu is still open. Unfortunately, there doesn't seem to be any way to detect that the user clicked "Open in new tab", and thus there's no way for me to close the menu in response. I can close the menu in response to the user's right click (step #2 above), but then they won't be able to pick "Open in new tab" because the link will be gone.
I've tried jQuery's click, mouseup, mousedown, and contextmenu events, but none of them seem to catch this case.
Has anyone else figured out a solution to this? Alternatively, if it's impossible, can anyone more knowledgeable than me definitively explain why? My guess is the browser just doesn't tell you about the click in step #4, but I have no real basis for that guess.