AnswerThat’s a good question. I’d love to say “yes”. I can’t.
(*: ignoring the question of whether browsers really implement their JS engines using one OS-thread, or whether other limited threads-of-execution are introduced by WebWorkers.)
However, in reality this isn’t quite true , in sneaky nasty ways.
The most common case is immediate events. Browsers will fire these right away when your code does something to cause them:
Results in log in, blur, log out on all except IE. These events don’t just fire because you called focus() directly, they could happen because you called alert() , or opened a pop-up window, or anything else that moves the focus.
This can also result in other events. For example add an i.onchange listener and type something in the input before the focus() call unfocuses it, and the log order is log in, change, blur, log out , except in Opera where it’s log in, blur, log out, change and IE where it’s (even less explicably) log in, change, log out, blur .
Similarly calling click() on an element that provides it calls the onclick handler immediately in all browsers (at least this is consistent!).
(I’m using the direct on… event handler properties here, but the same happens with addEventListener and attachEvent .)
There’s also a bunch of circumstances in which events can fire whilst your code is threaded in, despite you having done nothing to provoke it. An example:
Hit alert and you’ll get a modal dialogue box. No more script executes until you dismiss that dialogue, yes? Nope. Resize the main window and you will get alert in, resize, alert out in the textarea.
You might think it’s impossible to resize a window whilst a modal dialogue box is up, but not so: in Linux, you can resize the window as much as you like; on Windows it’s not so easy, but you can do it by changing the screen resolution from a larger to a smaller one where the window doesn’t fit, causing it to get resized.
You might think, well, it’s only resize (and probably a few more like scroll ) that can fire when the user doesn’t have active interaction with the browser because script is threaded. And for single windows you might be right. But that all goes to pot as soon as you’re doing cross-window scripting. For all browsers other than Safari, which blocks all windows/tabs/frames when any one of them is busy, you can interact with a document from the code of another document, running in a separate thread of execution and causing any related event handlers to fire.
Places where events that you can cause to be generated can be raised whilst script is still threaded:
- when the modal popups ( alert , confirm , prompt ) are open, in all browsers but Opera;
- during showModalDialog on browsers that support it;
- the “A script on this page may be busy...” dialogue box, even if you choose to let the script continue to run, allows events like resize and blur to fire and be handled even whilst the script is in the middle of a busy-loop, except in Opera.
- a while ago for me, in IE with the Sun Java Plugin, calling any method on an applet could allow events to fire and script to be re-entered. This was always a timing-sensitive bug, and it's possible Sun have fixed it since (I certainly hope so).
- probably more. It's been a while since I tested this and browsers have gained complexity since.
If the worst comes to the worst, you can solve concurrency problems by indirecting all event responses. When an event comes in, drop it in a queue and deal with the queue in order later, in a setInterval function. If you are writing a framework that you intend to be used by complex applications, doing this could be a good move. postMessage will also hopefully soothe the pain of cross-document scripting in the future.