3

I discovered this bug while dealing with another issue. The order of JavaScript commands listed is different than the order of their execution in 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. 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.

Here's the fiddle:

http://jsfiddle.net/akJD7/

Can anyone confirm that they see this and if so, tell me why this is happening?

Brian Duncan
  • 1,176
  • 1
  • 13
  • 21
  • 1
    My guess is the 2nd alert fires after document.write has returned but before the text has been rendered. The 2nd alert freezes browser execution, so rendering doesn't complete until after you dismiss the 2nd alert. – Madbreaks Jan 04 '13 at 23:06
  • Try replacing `document.write` with `console.log` and see if you see the same thing. Er..that is if Safari has a console available. – Madbreaks Jan 04 '13 at 23:08
  • I actually used document.write to have a jQuery-less example, but perhaps the same principle applies. Here's an example with jQuery: http://jsfiddle.net/VqzzU/ – Brian Duncan Jan 04 '13 at 23:16

2 Answers2

3

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, e.g. 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);

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • They were both good answers, I just picked this one because this is what I ended up going with. My actual use case didn't have any alerts at all ... it was just a $.show() call inside of a form submit handler. Works like a champ. Thanks! – Brian Duncan Jan 04 '13 at 23:24
2

Try this, if you have 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).

Madbreaks
  • 19,094
  • 7
  • 58
  • 72