How to find out if a dialog is on top (or relative z-order)

How to find out if a dialog is on top (or relative z-order)


My page opens multiple dialogs. If a user clicks an element within
one, I automatically close the dialog. However I only want this to
happen if the dialog is the top-most dialog, otherwise the default
behaviour (of bringing the dialog to the fore) should remain.
Is there any way to determine if a dialog is currently the top-most
one?