Safari 5.1 和 Javascript recognize() 函数的错误
下面更新
我想我刚刚在 OS X Lion 上的 Safari 5.1 中发现了一个非常奇怪的错误。我无法确定此错误是否存在于早期版本的 Safari 中,但我有点怀疑不存在(考虑到我的网站中引用此问题的部分曾经在 Lion 之前的 Safari 中运行)。我在 Chrome、Firefox 6、IE7 或 IE9 中没有看到该错误。我还能够确认该错误不会发生在 Windows 7 上的 Safari 5.0.2 中,这可能意味着这是 Safari 5.1 中的新功能。
所以这里有一个错误:如果 javascript recognize() 函数位于另一个函数内部,那么该函数中调用的任何内容在确认()之前都不会执行,直到“OK”或“单击“取消”按钮。对我来说,这确实是一个巨大且非常奇怪的错误。这是我的示例代码:
<!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 仅在单击“确定”或“取消”后出现。
另外,如果我将confirm()函数放在子函数中,如下所示:
function confirmwrapper() {
if ( confirm('Are you sure?') ) {
console.log(1);
}
}
function myexit () {
console.log(0);
confirmwrapper();
}
...它仍然会中断。
我在这里缺少一些基本的东西吗?恕我直言,像“只使用 JQuery/Mootools/其他一些很酷的 JS 库”这样的答案可能对我没有多大帮助,因为这段代码是遗留系统的一部分,我现在无法完全分解它,就像我一样想要。
更新:我刚刚测试了一个简单的 AJAX 请求(Mootools Request(),对于那些记分的人,尽管我认为这并不重要),并且我能够确认该请求未被执行直到确认对话框关闭——通过“确定”或“取消”。这一定是一个错误。坚果。
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此时我有点自言自语,但我已经有了“解决方案”(当我说“解决方案”时,我真正的意思是“似乎有效的黑客”),因为我无法使用模态对话来自 JS 库。如果我将confirm()调用放在另一个函数中,并通过setTimeout调用来调用它,这似乎给Safari提供了足够的喘息空间,以允许confirm()之前的代码执行。下面是示例:
需要明确的是,“logout()”是在用户操作(单击按钮)时调用的函数。
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:
To be clear, "logout()" is the function which gets invoked on user action (button click).