简体   繁体   English

JavaScript命令在Safari中不按顺序执行

[英]JavaScript commands not executed in order in Safari

I discovered this bug while dealing with another issue. 我在处理另一个问题时发现了这个bug。 The order of JavaScript commands listed is different than the order of their execution in Safari: 列出的JavaScript命令的顺序与它们在Safari中执行的顺序不同:

Example: 例:

alert('here');
document.write('This is the hidden message.');
alert('You should be seeing the hidden message by now.');

In my browser the alerts are executed before the document.write() statement. 在我的浏览器中, alertsdocument.write()语句之前执行。 I've seen this bug on two different Mac OS X's using Safari versions 5.17, 6.0 and 6.0.2, but I haven't confirmed that anyone else has seen this yet. 我在使用Safari版本5.17,6.0和6.0.2的两个不同的Mac OS X上看到了这个错误,但我还没有确认其他人已经看过这个。

Here's the fiddle: 这是小提琴:

http://jsfiddle.net/akJD7/ http://jsfiddle.net/akJD7/

Can anyone confirm that they see this and if so, tell me why this is happening? 任何人都可以确认他们看到了这个,如果有,请告诉我为什么会这样?

I don't think that's a bug, strictly speaking. 严格来说,我认为这不是一个错误。 It's just that it's all synchronous, and there was no repaint before the second alert. 只是它是全部同步的,并且在第二次警报之前没有重新绘制。 Repaints don't usually happen within the same "tick" of the browser's event loop (although document.write seems to force a repaint in other browsers, eg Chrome). 重绘通常不会发生在浏览器事件循环的同一“滴答”内(尽管document.write似乎强制在其他浏览器中重新绘制,例如Chrome)。

This (ugly) workaround should fix it: 这个(丑陋的)解决方法应该解决它:

alert('here');
document.write('This is the hidden message.');
setTimeout(function() {
    alert('You should be seeing the hidden message by now.');
}, 0);

Try this, if you have jQuery: http://jsfiddle.net/2Kcuz/ 试试这个,如果你有jQuery: http//jsfiddle.net/2Kcuz/

Per my comment, my guess is the text you added with document.write simply hasn't rendered yet (but it's there nonetheless). 根据我的评论,我的猜测是你用document.write添加的文本还没有渲染(但它仍然存在)。

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

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