Reputation: 3153
UPDATE BELOW
I think I've just found a really weird bug in Safari 5.1, on OS X Lion. I can't confirm for sure whether this bug existed on earlier versions of Safari, but I sort of suspect not (given the fact that the part of my site that references this issue used to work in pre-Lion Safari). I don't see the bug in Chrome, Firefox 6, IE7 or IE9. I've also been able to confirm that the bug does not happen in Safari 5.0.2 on Windows 7, which likely means this is new to Safari 5.1.
So here's the bug: it would appear that if the javascript confirm() function is inside of another function, then anything within that function called before confirm() is not executed until either the "OK" or "Cancel" button is clicked. That sure seems like a huge, and really weird, bug to me. Here's my sample code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function myexit () {
console.log(0);
if ( confirm('Are you sure?') ) {
window.close();
}
}
</script>
</head>
<body>
<a href="#" onclick="myexit();">close window</a>
</body>
</html>
In all the other browsers I mention, the 0 appears in the console at the same time the modal confirm box appears. In Safari, the 0 appears only after clicking either "OK" or "Cancel".
Also, if I put the confirm() function in a sub-function, like this:
function confirmwrapper() {
if ( confirm('Are you sure?') ) {
console.log(1);
}
}
function myexit () {
console.log(0);
confirmwrapper();
}
...it still breaks.
Am I missing something basic here? Respectfully, answers like "just use JQuery/Mootools/some other cool JS library" probably don't help me all that much, because this code is part of a legacy system that I can't completely rip apart right now, much as I want to.
UPDATE: I just tested a simple AJAX request (Mootools Request(), for those keeping score, though I don't think it really matters), and I was able to confirm that the Request is not executed until the confirm dialog is closed--either via "OK" or "Cancel". That's gotta be a bug. Nuts.
Thanks in advance
Upvotes: 3
Views: 4396
Reputation: 3153
I'm sort of talking to myself at this point, but I've got "solution" (and when I say "solution" I really mean "hack that appears to work"), given that I can't use a modal dialogue from a JS library. If I put the confirm() call in another function, and invoke that with a setTimeout call, that appears to give Safari enough breathing room to allow the code before confirm() to execute. Here's the example:
function doconfirm() {
if ( confirm('Are you sure you want to logout?') ) {
window.top.close();
}
}
function logout () {
doCourseSave();
window.setTimeout('doconfirm();', 500);
}
To be clear, "logout()" is the function which gets invoked on user action (button click).
Upvotes: 2