When the escape key is pressed, I would like to close a child window, but first check for open overlays. If an overlay is open and the escape key is pressed, I only want the overlay to close. If the user presses escape again, I want it to close the child window.
So, I first need to check whether an overlay is open. All my overlays have a class of .simpleOverlay and a close button with the class .closeOverlayBtn, so I thought of having a function that checks if that class has focus, before closing the window, like so:
if (e.keyCode == 27) { // "Esc" Key
if ( $('.simpleOverlay').is(':focus') ) {
$('.closeOverlayBtn').trigger('click');
}
else {
window.close();
}
return(false);
}
But this is not happening!! If I replace the .trigger line with an alert(), I get alerted when the overlay is closed and open, not good!
Is this the right way to go about this or am I completely missing something?
Try this: