Safari 5.1和Java语言Confirm()函数的错误 [英] Bug with Safari 5.1 and Javascript confirm() function

查看:58
本文介绍了Safari 5.1和Java语言Confirm()函数的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面更新

我认为我刚刚在OS X Lion的Safari 5.1中发现了一个非常奇怪的错误.我无法确定该错误是否在Safari的早期版本中存在,但我有点怀疑(鉴于我的网站中引用此问题的部分曾经在Lion Safari之前的版本中起作用).我没有在Chrome,Firefox 6,IE7或IE9中看到该错误.我还能够确认Windows 7上的Safari 5.0.2中没有发生该错误,这可能意味着这是Safari 5.1的新功能.

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.

这就是错误:如果javascript Confirm()函数位于另一个函数内部,则该函数中称为 before confirm()的任何内容都不会执行,除非其中一个确定"单击或"取消按钮.对我来说,那肯定是一个巨大的,而且很奇怪的错误.这是我的示例代码:

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>

在我提到的所有其他浏览器中,控制台中出现模式确认框的​​同时显示0.在Safari中,仅在单击确定"或取消"后的 中显示0.

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".

此外,如果将confirm()函数放在子函数中,如下所示:

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();
}

...它仍然坏了.

我在这里缺少基本的东西吗?顺便说一句,诸如仅使用JQuery/Mootools/其他酷炫的JS库"之类的答案可能对我没有多大帮助,因为该代码是旧系统的一部分,我现在无法像现在这样完全拆开.想.

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.

更新:我只是测试了一个简单的AJAX请求(Mootools Request(),用于保持得分,尽管我认为这并不重要),并且我能够确认该请求不是 直到关闭确认对话框-通过确定"或取消".那一定是个错误.坚果.

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.

预先感谢

推荐答案

在这一点上,我有点在自言自语,但是我有解决方案"(当我说解决方案"时,我的意思是黑客"似乎可以正常工作"),因为我无法使用JS库中的模式对话.如果我将confirm()调用放到另一个函数中,并通过setTimeout调用来调用它,这似乎为Safari提供了足够的喘息空间,以允许在confirm()执行之前执行代码.这是示例:

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);
}

要清楚,"logout()"是在用户操作(单击按钮)时调用的函数.

To be clear, "logout()" is the function which gets invoked on user action (button click).

这篇关于Safari 5.1和Java语言Confirm()函数的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆