Some call it JSONP , others On Demand Javascript
Version A
- The returned data looks like: myData = { ... } Details in the box below.
- A repeated timer is used to detect if the data has arrived or if the request failed.
- Successfully tested in: IE7, FF3, Chrome, Opera.
- Check out also these versions:
- Version B - returned data looks like myData = { ... }, uses event handlers to detect a failure earlier.
- Version C - returned data looks like processData({ ... }), uses a callback function and a single timer to detect a failure.
Klick to load data - case 1: should work just fine, immediate response
Klick to load data - case 2: simulates a slow response, it takes about 10 seconds for the data to arrive, whereas the timeout is chosen to be 5 seconds
Klick to load data - case 3: simulates an internal server error (error 500)
note that we have to wait until the timeout occurs, even though the error shows up almost immediately, that is because "myData" is not defined
Klick to load data - case 4: simulates server down / not reachable
|
|
the returned data
room for error message
|
Recommendation:
In the usual case of no error version C with the callback function is the fastest, since the data is processed immediately when it arrives. Version A and B are not much slower, but need the additional overhead of a repeated timer.
In the case of failure, version B is the fastest by far. The failure is picked up by the event handlers, where the other versions just sit and wait until some time limit is up and then check if the data arrived correctly. However, different browsers use different event handlers, so extra work has to be done. Opera 9.6 does not seem to fire the onerror event correctly (Version B, case 3 and 4). But no harm is done, the timeout catches the failure at last.
Conclusion: The differences are small. If everything works as expected all versions seem reasonable. That said, if browser compatibilty is most important to you, use version C. Otherwise go with version B.
Comment are welcome and might appear here after a while unless you wish otherwise (sorry my page is not set up to do this automatically).