On 1-Dec-09, at 7:51 AM, Lex Spoon wrote:
> On Mon, Nov 30, 2009 at 10:28 PM, Matt Mastracci <matt...@mastracci.com
> > wrote:
>> 2. onerror works some of the time in some of the browsers. It
>> fails on
>> various combinations of resolve errors, error status codes and
>> other failure
>> conditions. For all browsers (except Opera) that don't support it
>> directly,
>> It can be emulated with onreadystatechange/onload and lack of a JSONP
>> callback.
>
> Can you expand on that? IE has script-tag callbacks that should be
> usable to detect download errors. What did you get working on other
> browsers?
>
> If there's a way to detect download failures on Firefox and on
> Webkit-based browser, then JSONP downloads are better than I thought.
onerror is reasonably reliable across modern browsers. For IE, you can
use onreadystatechange to detect that the script finished loading and
then check to see if a JSONP callback wasn't made or a marker global
wasn't initialized:
Firefox 2/3/3.5: onerror will fire
Safari 3/4: onerror will fire
Safari 2: no events will fire
Chrome: onerror will fire
IE6/7/8: onreadystatechange(loaded) will fire, but JS won't be loaded
Opera: no events will fire
As mentioned before, I couldn't get Opera to tell me when a script
failed. Safari 2 also seemed to ignore error conditions, but its
overall browser share is negligible.
All of the browsers that responded to errors threw the same errors
when retrieving a 404 response or an error while resolving a domain.
I've seen some notes elsewhere that inferred that some browsers
treated some error conditions differently, but I couldn't reproduce
that in testing.
Note that the events only seem to work consistently across browsers
when assigning a function to the onevent properties before the src is
assigned. This is basically the code I used for my test:
function testLoad() {
!!marker ? success() : error();
}
var script = document.createElement('script');
script.onreadystatechange = function(e) {
if (this.readyState == 'loaded')
testLoad();
}
script.onload = function(e) {
testLoad();
}
script.onerror= function(e) {
error(e);
}
script.src = 'notfound.js';
document.body.insertBefore(script, null);
Matt.
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors