[whatwg] script element onerror event

On Sat, 28 May 2011, John J. Barton wrote:
> To allow optional JavaScript download, some widely used JavaScript 
> libraries, such as jQuery and requireJS, use script elements added to 
> the document dynamically by JavaScript. (Of course this feature is also 
> used by applications directly as well).  For normal deployment this 
> approach works well in practice. At development time however, or in the 
> presence of network or server problems, the approach gives poor error 
> recovery information. Fundamentally the problem is that the insertion 
> mechanism has no error return path.
> The script element does support one event, 'onload' which fires after 
> the script has finished loading. I suggest the addition of one new 
> event, 'onerror', which fires in every other case. For examples, a 
> network error (4XX, 5XX) or JavaScript parse error would trigger onerror 
> but not onload.  On the other hand, a runtime error for the 
> outer-function of the script element would trigger onload (I guess), but 
> the developer can handle this with try/catch.
> Very long load times would still have poor error recovery information, 
> but developers could implement UI to signal "loading..." once they know 
> they will get some update event eventually.

On Sun, 29 May 2011, Mike Wilson wrote:
> This event is actually already speced, see #14 "fire a simple event 
> named error at the element" in: 
> http://www.whatwg.org/specs/web-apps/current-work/#prepare-a-script (and 
> the onerror attribute is valid for all elements)

On Sun, 29 May 2011, John J. Barton wrote:
> Step 14 is unclear or incomplete however:
> " If the|src
> <http://www.whatwg.org/specs/web-apps/current-work/#attr-script-src>|attribute's
> value is the empty string or if it could not be resolved,..."
> Does this mean the error handler will be called in the case of 4XX, 5XX, 
> and syntax errors?

It's not incomplete, it's just that you don't know you got a 4xx/5xx at 
that step, you only know after you've hit the network. The 'error' event 
is fired lower down for that case.

On Sun, 29 May 2011, John J. Barton wrote:
> (I've not seen a "for" attribute on a script element. Is there any 
> documentation on what it does?)

It doesn't do anything useful per spec. It's an old IEism.

Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Friday, 10 June 2011 15:56:04 UTC