<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>David Lynch &#187; cross-browser</title>
	<atom:link href="http://davidlynch.org/blog/tag/cross-browser/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidlynch.org/blog</link>
	<description>has a blog</description>
	<lastBuildDate>Fri, 18 Jun 2010 07:37:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Detecting failure</title>
		<link>http://davidlynch.org/blog/2009/02/detecting-failure/</link>
		<comments>http://davidlynch.org/blog/2009/02/detecting-failure/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 09:11:01 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[cross-browser]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://davidlynch.org/blog/?p=58</guid>
		<description><![CDATA[I warn you in advance that this post does not end with a resolution of my problems. For reasons relating to dynamic loading of javascript dependencies, I wanted to find a way to tell: When a script tag finishes loading a file Whether that file was successfully loaded For various reasons, I didn&#8217;t want to [...]]]></description>
			<content:encoded><![CDATA[<p>I warn you in advance that this post does not end with a resolution of my problems.</p>
<p>For reasons relating to dynamic loading of javascript dependencies, I wanted to find a way to tell:</p>
<ol>
<li>When a script tag finishes loading a file</li>
<li>Whether that file was successfully loaded</li>
</ol>
<p>For various reasons, I didn&#8217;t want to add cruft into the files being loaded &#8212; no appending a function call to the end of every file, or anything.</p>
<p>Now, the finishes-loading case turns out to be pretty easy, albeit with some quirky cross-browser ramifications:</p>
<pre class="prettyprint"><code>var eax, load;
eax = document.createElement('script');
eax.setAttribute('type', 'text/javascript');
eax.setAttribute('src', src);
load = function(e) {
    // FF/Safari get the `load` handler, IE gets the `readystatechange` handler.
    // IE doesn't fire 'load' for JS (but does for CSS...)
    if(!eax.readyState || eax.readyState == 'complete' || eax.readyState == 'loaded') {
        // IE6 stalls at 'loaded' sometimes
        alert('loaded!');
        // remove these listeners for everyone, because it's
        // easier than testing everything to find out whether it's needed.
        removeEventListener(eax, 'readystatechange', arguments.callee);
        removeEventListener(eax, 'load', arguments.callee);
    }
};
addEventListener(eax, 'load', load); // FF/Safari get this
addEventListener(eax, 'readystatechange', load); // IE gets this</code></pre>
<p>It&#8217;s a horrible mish-mash of events, obviously, but it works.  Insofar as it goes.</p>
<p>Working out whether the load was <em>successful</em> turns out to be the hard part.</p>
<p>In Firefox it&#8217;s very easy.  The <code>load</code> event doesn&#8217;t fire if there are problems loading the JS file, and a <code>error</code> event fires instead.  This is <em>lovely</em>.</p>
<p>In Safari the <code>load</code> event doesn&#8217;t fire if there are problems, but there&#8217;s no other sign given.  So I could probably fake this with a <code>setTimeout</code> set to a reasonable length &#8212; not perfect, but good enough for most cases.</p>
<p>In IE the <code>readystatechange</code> event fires away regardless.  It&#8217;s here that I&#8217;m stuck &#8212; I can&#8217;t see any way to tell, in the <code>readystatechange</code> handler whether the script tag was really loaded without problems.</p>
<p>Since IE represents an unfortunately large component of deviantART&#8217;s users, half-working failure detection isn&#8217;t going to cut it.  Especially since all the developers mainly use Firefox/Safari, and wouldn&#8217;t expect IE to behave differently.</p>
<p>So for now I&#8217;m going with verifying that the script tag loaded something, and says it&#8217;s complete.  I&#8217;ll keep my eyes out for a way to work around IE&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlynch.org/blog/2009/02/detecting-failure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
