At the moment with work I am refactoring webframework to provide a better layer to, in turn, upgrade and re-engineer an existing platform. Part of this work is to provide an AJAX layer because everyone loves AJAX. The advantage of the approach I am using is that it becomes very easy to use AJAX with components and custom scripted actions. The details are for another day, but I did stumble across an odd behaviour which I wanted to detail here.
This concerns ordering of code, the submission of an AJAX request and the way AJAX responses are handled.
My code was originally structured as follows (and works correctly in Safari):
function doAJAXRequest() {
Setup AJAX Request
Post AJAX Request
Setup Callbacks Relating To Request
Setup visual indicators
}
The effect is that the request is built, sent and whilst waiting for a response I setup some parts that would be needed upon return of the request as well as a visual indicator that a request had been sent. This works with Safari but not Firefox on fast connections. If the request is processed quickly, the response is processed within the onchange handler for the AJAX object before the visual indicator has been shown, or some extra callbacks setup, which in turns causes all manner of errors. I think this is because the onchange can be called at anytime in Firefox (it will interrupt a running function), whereas Safari waits until the runtime has returned to its event loop (it wont interrupt a running function).
This difference in processing is subtle, and with carefully structured code should not arise on a common basis. I found the differences between the Javascript runtimes of Firefox and Safari different enough to warrant noting it on my site.