In the web site I am writing there is the need for some confirmation text to be shown before something happens. The solution I am using shows a modal window on the button click and when the window is closed the Jquery looks at which button was clicked.
So I have the following for a logout confirmation
$(function () {
$('#logOut').on('click', function () {
url = $(this).data('request-url');
$('#modalLogout').modal('show');
});
$('#modalLogout').on('hide.bs.modal', function () {
var activeElement = $(document.activeElement);
if (activeElement.is('[data-toggle], [data-dismiss]')) {
if (activeElement[0].id === "LogoutOk") {
window.location.href = url;
}
}
});
});
and the modal window
<div id="modalLogout" class="modal fade" role="dialog">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-body">
<p>Do you want to log out?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="LogoutCancel">Cancel</button>
<button type="button" class="btn btn-warning" data-dismiss="modal" id="LogoutOk">OK</button>
</div>
</div>
</div>
</div>
In windows I have tested the above in IE, Edge, Firefox and Chrome and it all works as I would expect it to - when the modal window closes the user is logged out only if the OK button was pressed, clicking anywhere else will leave you logged in.
On a Mac using Safari, clicking OK will do nothing. Looking at the console, the problem appears to be the $(document.activeElement)
. In a windows browser this will return the button that was clicked, in Safari I get the the whole web page starting with the <body>
.
Reading around the net there seems to be a (small) amount of discussion on the subject of activeElement in Safari, but not as much as I would expect if a lot of people are doing something similar. None of the solutions I have looked at are applicable in my case.
Is there a way to get the clicked button in Safari, or is there a better way to achieve what I am after that will work in all browsers?