Reputation: 35725
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 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.
Upvotes: 1
Views: 2111
Reputation: 119877
you could check if the window lost focus, and react appropriately after that (which is to close your menu)
but this assumes that the browser was set to "switch to new tab" by default. some users might choose the setting of opening the new tab in the background, so the main window does not lose focus and this method will not work. (but hey, who wants to close the menu if i am still looking at it anyway?)
Upvotes: 1