<?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; javascript</title>
	<atom:link href="http://davidlynch.org/blog/tag/javascript/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>
		<item>
		<title>del.icio.us plus 1.1</title>
		<link>http://davidlynch.org/blog/2008/04/delicious-plus-11/</link>
		<comments>http://davidlynch.org/blog/2008/04/delicious-plus-11/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 21:18:51 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[del.icio.us]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://davidlynch.org/blog/?p=36</guid>
		<description><![CDATA[New version of my del.icio.us widget. The only real change is that it now allows for multiple instances of the widget. So you can have one showing the &#8216;food&#8217; tag, and the other showing the &#8216;yodeling&#8217; tag, say.]]></description>
			<content:encoded><![CDATA[<p>New version of my 
<a  href="http://wordpress.org/extend/plugins/delicious-plus/">del.icio.us widget</a>.</p>
<p>The only real change is that it now allows for multiple instances of the widget.  So you can have one showing the &#8216;food&#8217; tag, and the other showing the &#8216;yodeling&#8217; tag, say.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlynch.org/blog/2008/04/delicious-plus-11/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Topsy turvy</title>
		<link>http://davidlynch.org/blog/2008/03/topsy-turvy/</link>
		<comments>http://davidlynch.org/blog/2008/03/topsy-turvy/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 04:57:30 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://davidlynch.org/blog/?p=28</guid>
		<description><![CDATA[A bug report for maphilight lead to me becoming aware of a fascinating quirk in IE. A quirk in which IE holds to published standards with fanatical zeal, contrary to everything one might have come to expect, and far in excess of Firefox/Opera/Safari. When you use the .innerHTML property to add an element to the [...]]]></description>
			<content:encoded><![CDATA[<p>A bug report for 
<a  href="http://plugins.jquery.com/project/maphilight">maphilight</a> lead to me becoming aware of a fascinating quirk in IE.  A quirk in which IE holds to published standards with fanatical zeal, contrary to everything one might have come to expect, and far in excess of Firefox/Opera/Safari.</p>
<p>When you use the <code>.innerHTML</code> property to add an element to the DOM, IE will fire an &#8220;unknown runtime error&#8221; if that element is incorrectly nested.  So trying to place a <code>&lt;div&gt;</code> inside a <code>&lt;p&gt;</code> (as was the case in the bug report) will error very unhelpfully.</p>
<p>Surprising behavior.</p>
<p>Anyway, this led to the release of 
<a  href="http://plugins.jquery.com/files/maphilight-1.1.1.tar.gz">maphilight 1.1.1</a>.  (Which also includes an official minified version of the file, for convenience&#8217;s sake.)</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlynch.org/blog/2008/03/topsy-turvy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>del.icio.us widget</title>
		<link>http://davidlynch.org/blog/2008/03/delicious-widget/</link>
		<comments>http://davidlynch.org/blog/2008/03/delicious-widget/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 19:21:45 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[del.icio.us]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://davidlynch.org/blog/?p=26</guid>
		<description><![CDATA[I couldn&#8217;t find a WordPress widget that produced output similar to del.icio.us&#8217;s linkrolls script. (You can just put their script into a Text widget, but its output doesn&#8217;t always mesh well with WordPress themes &#8212; it hardcodes h2s, and so forth.) The Automattic example widget came close, but was a bit lacking on the customization [...]]]></description>
			<content:encoded><![CDATA[<p>I couldn&#8217;t find a WordPress widget that produced output similar to 
<a  href="http://del.icio.us/help/linkrolls">del.icio.us&#8217;s linkrolls script</a>.  (You <em>can</em> just put their script into a Text widget, but its output doesn&#8217;t always mesh well with WordPress themes &#8212; it hardcodes h2s, and so forth.)</p>
<p>The 
<a  href="http://automattic.com/code/widgets/">Automattic example widget</a> came close, but was a bit lacking on the customization front.</p>
<p>
<a  href="http://rick.jinlabs.com/code/delicious/">del.icio.us for WordPress</a> also came close, but I disagree with having your server fetch the RSS feed, instead of using the perfectly good JSON API.</p>
<p>So I took the Automattic widget, hacked some extra features into it, made it configurable, and you can now see it in my sidebar.  Well, footer currently.  It&#8217;s a theme thing.</p>
<p>If you want it yourself, here&#8217;s the link:  
<a  href='http://wordpress.org/extend/plugins/delicious-plus/'>del.icio.us plus</a></p>
<p><ins>Edit on 2008-03-27: This is now in the wordpress.org plugins repository.</ins><br />
<ins>Edit on 2008-04-16: Version 1.1 released, granting multiple widget instances.</ins></p>
]]></content:encoded>
			<wfw:commentRss>http://davidlynch.org/blog/2008/03/delicious-widget/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>maphilight: image map mouseover highlighting</title>
		<link>http://davidlynch.org/blog/2008/03/maphilight-image-map-mouseover-highlighting/</link>
		<comments>http://davidlynch.org/blog/2008/03/maphilight-image-map-mouseover-highlighting/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 04:30:06 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://davidlynch.org/blog/2008/03/maphilight-image-map-mouseover-highlighting/</guid>
		<description><![CDATA[UPDATE 2010-05-22: Version 1.2 released. Works in IE8. I just released maphilight, a jQuery plugin that turns image maps into wonderful graphical masterpieces. Image maps aren&#8217;t so popular any more, for some strange reason. So a quick definition: an imagemap is an &#60;img&#62; with the usemap attribute, pointing to a &#60;map&#62; that describes polygons that [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE 2010-05-22:</strong> 
<a  href="http://davidlynch.org/blog/2010/05/maphilight-1-2/">Version 1.2 released</a>. Works in IE8.</p>
<p>I just released 
<a  href="http://plugins.jquery.com/project/maphilight">maphilight</a>, a jQuery plugin that turns image maps into wonderful graphical masterpieces.</p>
<p>Image maps aren&#8217;t so popular any more, for some strange reason.  So a quick definition: an imagemap is an <code>&lt;img&gt;</code> with the <code>usemap</code> attribute, pointing to a <code>&lt;map&gt;</code> that describes polygons that link places within that image.</p>
<p>This sprung from me wanting to display pretty highlighting of countries on a map, but not wanting to mess with flash for it.  It involves enough annoying fiddling with <code>&lt;canvas&gt;</code> (and VML, because IE is in the stone age) that I feel I&#8217;m saving other people a decent amount of work by releasing it.</p>
<p>
<a  href="http://davidlynch.org/js/maphilight/docs/demo_simple.html">Simple demo</a><br />

<a  href="http://davidlynch.org/js/maphilight/docs/demo_world.html">Pretty demo using a world map</a><br />

<a  href="http://davidlynch.org/js/maphilight/docs">Documentation</a><br />

<a  href="http://davidlynch.org/js/maphilight/jquery.maphilight.js">Download</a> (requires 
<a  href="http://jquery.com">jQuery</a>)</p>
<p>(Tools like &#8220;
<a  href="http://www.kolchose.org/simon/ajaximagemapcreator/">HTML-Image map Creator WYSIWYG</a>&#8221; might be handy if you want to make image maps yourself.)</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlynch.org/blog/2008/03/maphilight-image-map-mouseover-highlighting/feed/</wfw:commentRss>
		<slash:comments>75</slash:comments>
		</item>
		<item>
		<title>Fixing sortForce in jQuery&#8217;s tablesorter</title>
		<link>http://davidlynch.org/blog/2008/02/fixing-sortforce-in-jquerys-tablesorter/</link>
		<comments>http://davidlynch.org/blog/2008/02/fixing-sortforce-in-jquerys-tablesorter/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 22:49:47 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://davidlynch.org/blog/2008/02/fixing-sortforce-in-jquerys-tablesorter/</guid>
		<description><![CDATA[jQuery has a table-sorting plugin, part of their official UI project. It&#8217;s quite a nice table-sorting library, handling the common cases, with options making it configurable to suit many people&#8217;s needs. However, I ran into a problem when using it in a project. The documentation and the functionality don&#8217;t quite line up. It has an [...]]]></description>
			<content:encoded><![CDATA[<p>
<a  href="http://www.jquery.com">jQuery</a> has a 
<a  href="http://tablesorter.com/docs/" title="tablesorter">table-sorting plugin</a>, part of their 
<a  href="http://ui.jquery.com/">official UI project</a>.  It&#8217;s quite a nice table-sorting library, handling the common cases, with options making it configurable to suit many people&#8217;s needs.</p>
<p><em>However</em>, I ran into a problem when using it in a project.  The documentation and the functionality don&#8217;t quite line up.</p>
<p>It has an option, sortForce, which its documentation says you can use to &#8220;add an additional forced sort that will be appended to the dynamic selections by the user&#8221;.  This is a handy concept &#8212; it lets you, say, keep records ordered by name, regardless of which other criteria the user chooses to sort by.</p>
<p>The problem is that it actually <em>prepends</em> the sort to the user&#8217;s selection, which means that the user is restricted to sorting within the forced sort.  (This is also a potentially useful tool; it&#8217;s just not what the documentation indicates.)</p>
<p>So I wrote up 
<a  href='http://davidlynch.org/js/jquery.tablesorter.sortForce_optimal.patch' title='jQuery Tablesorter sortForce patch'>a patch</a> that fixes this, along with a few other niggling issues with sortForce.  (Its interaction with the user sorting by multiple columns, and it locking the forced-sort column in one sort direction.)  To preserve backwards-compatibility I added a new option, sortAppend, to provide the documented behavior.</p>
<p>I also submitted the patch to the maintainer, so hopefully it can get incorporated.</p>
<p><ins>2008-08-27: My patch was incorporated as of version 2.0.2, so it&#8217;s all good.</ins><br />
<ins>2009-03-17: But bits of it weren&#8217;t applied, so it can&#8217;t be said to have been fixed. Oh well. I&#8217;ll resubmit.</ins></p>
]]></content:encoded>
			<wfw:commentRss>http://davidlynch.org/blog/2008/02/fixing-sortforce-in-jquerys-tablesorter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Practicing JavaScript with Dilbert</title>
		<link>http://davidlynch.org/blog/2008/01/practicing-javascript-with-dilbert/</link>
		<comments>http://davidlynch.org/blog/2008/01/practicing-javascript-with-dilbert/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 16:53:16 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[dilbert]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://davidlynch.org/blog/2008/01/practicing-javascript-with-dilbert/</guid>
		<description><![CDATA[I discovered that there was a flash widget displaying Dilbert archives in color, back to the start of 2007. Naturally I thought to myself &#8220;aha, there must be an XML data feed somewhere in that!&#8221; Some light flash-decompilation later, I discovered that I was right. I then seized on this as a learning opportunity, and [...]]]></description>
			<content:encoded><![CDATA[<p>I 
<a  href="http://dilbertblog.typepad.com/the_dilbert_blog/2008/01/dilbert-widget.html">discovered</a> that there was a 
<a  href="http://widget.dilbert.com/">flash widget</a> displaying Dilbert archives in color, back to the start of 2007.</p>
<p>Naturally I thought to myself &#8220;aha, there must be an XML data feed somewhere in that!&#8221;  Some light flash-decompilation later, I discovered that I was right.</p>
<p>I then seized on this as a learning opportunity, and wrote a much better viewer, in boring old JavaScript.  Why is it better?  Because it doesn&#8217;t require Flash, and is not limited to one panel at a time, that&#8217;s why.</p>
<p>
<a  href="http://davidlynch.org/toys/dilbert/">My Dilbert viewer, can you view it?</a></p>
<p>Caveats:<br />
1. It&#8217;s perfect in Firefox3 and Safari.  Sundays aren&#8217;t <em>quite</em> right in IE7; the first and last buttons are missing in Opera.  All else is untested.<br />
2. For some reason, the widget seems to get different feeds for the last week or two, which means this viewer is a week behind.  I suspect user-agent sniffing, but have not yet been motivated to work out what a flash-player&#8217;s user-agent is.</p>
<p>Things learned:<br />
1. Cross-domain xmlhttprequest requiring a proxy is a pain, and has no obvious benefit.<br />
2. 
<a  href="http://www.jquery.com">jQuery</a> is still awesome.<br />
3. Forgetting to take out debug statements that rely on FireBug when uploading is dumb of me.<br />
4. Decompiling these things is complex &#8212; the embed code provided loads a .swf, which loads a .swf, which finally loads the actual widget that displays the comics.</p>
<p><ins>Update: I worked out the cause of the weird out-of-sync-ness I observed between the data files I received and the content seen in the official widget.  The lesson is not to trust the data feed when it tries to tell you which domain to look at; it includes a &lt;Domain&gt; tag, which seems like a perfect complement to the domain-less URLs given in the strip descriptions.  However, that domain contains files that are several weeks out of date.  So I just hardcoded the &#8220;real&#8221; URL.</ins></p>
<p><ins>They finally released this stuff to the main Dilbert site, so I just went through and fixed up the viewer to use the appropriate new feed format.  Darn changes.</ins></p>
]]></content:encoded>
			<wfw:commentRss>http://davidlynch.org/blog/2008/01/practicing-javascript-with-dilbert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
