简体   繁体   English

Safari 5.1和Javascript confirm()函数的错误

[英]Bug with Safari 5.1 and Javascript confirm() function

UPDATE BELOW 更新下面

I think I've just found a really weird bug in Safari 5.1, on OS X Lion. 我想我在OS X Lion上的Safari 5.1中发现了一个非常奇怪的错误。 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). 我无法确定这个错误是否存在于早期版本的Safari中,但我怀疑不是(考虑到我的网站中引用此问题的部分曾经在前Lion Safari中工作)。 I don't see the bug in Chrome, Firefox 6, IE7 or IE9. 我没有看到Chrome,Firefox 6,IE7或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. 我还能够确认在Windows 7上的Safari 5.0.2中没有发生该错误,这可能意味着这是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. 所以这就是错误:如果javascript confirm()函数在另一个函数内部,那么看起来 confirm() 之前调用的任何函数都不会执行,直到单击“确定”或“取消”按钮。 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. 在我提到的所有其他浏览器中,0出现在控制台中,同时出现模态确认框。 In Safari, the 0 appears only after clicking either "OK" or "Cancel". 在Safari中,只有单击“确定”或“取消” 才会显示0。

Also, if I put the confirm() function in a sub-function, like this: 另外,如果我将confirm()函数放在子函数中,如下所示:

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. 恭敬地,像“只使用JQuery / Mootools /其他一些很酷的JS库”这样的答案可能并没有帮助我这么多,因为这段代码是遗留系统的一部分,我现在不能完全撕裂,就像我想要。

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". 更新:我刚刚测试了一个简单的AJAX请求(Mootools Request(),对于那些保持得分的人,虽然我认为它不重要),并且我能够确认在确认对话框关闭之前执行请求 - - 通过“确定”或“取消”。 That's gotta be a bug. 那将是一个错误。 Nuts. 坚果。

Thanks in advance 提前致谢

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. 我现在正在谈论自己,但我有“解决方案”(当我说“解决方案”时,我的意思是“看似工作的黑客”),因为我不能使用模态对话来自JS库。 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. 如果我将confirm()调用放在另一个函数中,并使用setTimeout调用调用它,这似乎给Safari足够的喘息空间,允许在confirm()执行之前执行代码。 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). 要清楚,“logout()”是在用户操作(按钮单击)上调用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM