<?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>Understanding Nothing &#187; JavaScript</title>
	<atom:link href="http://nothing.tmtm.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://nothing.tmtm.com</link>
	<description>Tony Bowden's ramblings</description>
	<lastBuildDate>Mon, 16 Jan 2012 08:23:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>More on Perlish Javascript</title>
		<link>http://nothing.tmtm.com/2003/01/more-on-perlish-javascript/</link>
		<comments>http://nothing.tmtm.com/2003/01/more-on-perlish-javascript/#comments</comments>
		<pubDate>Sat, 25 Jan 2003 12:11:22 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=632</guid>
		<description><![CDATA[After my recent playings with JavaScript, it&#8217;s nice to see that Jon Udell has also been discovering that JavaScript can be written quite neatly also: I always thought Perl had a role to play on the desktop. And now that it has transfused its genes into JavaScript, it does.]]></description>
			<content:encoded><![CDATA[<p>After my recent <a href="http://www.tmtm.com/nothing/archives/2003_01_08.html">playings</a> with JavaScript, it&#8217;s nice to see that Jon Udell has also <a href="http://weblog.infoworld.com/udell/2003/01/21.html#a579">been discovering</a> that JavaScript can be written quite neatly also: <q>I always thought Perl had a role to play on the desktop. And now that it has transfused its genes into JavaScript, it does</q>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2003/01/more-on-perlish-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript</title>
		<link>http://nothing.tmtm.com/2003/01/object-oriented-javascript/</link>
		<comments>http://nothing.tmtm.com/2003/01/object-oriented-javascript/#comments</comments>
		<pubDate>Wed, 08 Jan 2003 10:50:48 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=624</guid>
		<description><![CDATA[I can see lots of similarities between JavaScript and Perl. Both are languages that are often written by people with no real programming experience, just to get a job done &#8211; usually involving web sites. A lot of the code in each isn&#8217;t written from scratch, but starts by taking some other code that almost [...]]]></description>
			<content:encoded><![CDATA[<p>I can see lots of similarities between JavaScript and Perl. Both are languages that are often written by people with no real programming experience, just to get a job done &#8211; usually involving web sites. A lot of the code in each isn&#8217;t written from scratch, but starts by taking some other code that almost does what you want, and hacking it around until it does do what you want. And, as a result, a lot of the code that exists in both languages is really ugly, clumsy, and contains lots of special case code and lots of subtle bugs. Which the next person to adapt the script hacks around until it does what they want. And so on.</p>
<p>But beneath it all, both are actually very powerful languages, which can be well written, clean, expressive, and well factored.</p>
<p>And whilst I&#8217;m perfectly at home writing Perl like that, my JavaScript skills are still rather lacking.</p>
<p>So, I was playing around with Object Oriented JavaScript over the holidays. I found a good example at <a href="http://www.chunkysoup.net/advanced/oojavascript1/">ChunkySoup</a>, but I still wasn&#8217;t entirely happy with the results.</p>
<p>The code for the <a href="http://www.chunkysoup.net/advanced/oojavascript1/finishedgallery.html">test page</a> is still a little uglier, and more repetitive than I&#8217;d like. In particular, each link on the page still needs to explicitly set up its<br />
own handling code for the image rollovers etc.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCB1428.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[0].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[0].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[0].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4337.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[1].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[1].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[1].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4358.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[2].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[2].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[2].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link4&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4373.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[3].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[3].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[3].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN1509.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[4].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[4].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[4].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>And setting up the JavaScript that gets called onLoad is a little repititive too:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elements <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> thumbnailID <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;thumbnail&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this is universal for the page</span>
<span style="color: #003366; font-weight: bold;">var</span> emptyimg <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;blank.gif&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this is universal for the page</span>
<span style="color: #003366; font-weight: bold;">var</span> photoID <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;bigimage&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this is universal for the page</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> initpage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN1428tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN1428.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4337tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4337.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4358tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4358.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4373tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4373.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN1509tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN1509.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So, I figured it should be possible to abstract some of that away further too. The JavaScript should be able to dynamically alter the DOM and set up all the event handles. Then the links could just be set up as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link1&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN1428.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link2&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4337.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link3&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4358.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link4&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4373.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link5&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN1509.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>And at the start of the page, I&#8217;d just want to associate images with each link:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> initpage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link1&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN1428.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN1428tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link2&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4337.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4337tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link3&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4358.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4358tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link4&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4373.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4373tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link5&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN1509.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN1509tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The addImage JavaScript would then find the image element with the id of the first parameter, associate an onclick() with the second element, and a rollover() with the third.</p>
<p>After a lot of playing around I ended up with a nice abstract JavaScript addImage function that does just this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> addImage<span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> img<span style="color: #339933;">,</span> thumb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> pno <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> csnPhotoObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;thumbnail&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;blank.gif&quot;</span><span style="color: #339933;">,</span>thumb<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;bigimage&quot;</span><span style="color: #339933;">,</span>img<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  img.<span style="color: #660066;">onmouseover</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> pno.<span style="color: #660066;">handleMouseOver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  img.<span style="color: #660066;">onmouseout</span>  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> pno.<span style="color: #660066;">handleMouseOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  img.<span style="color: #660066;">onclick</span>     <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> pno.<span style="color: #660066;">handleClick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>What I don&#8217;t like about this though, is the need to set up the anonymous closures. (Of course, before this I didn&#8217;t even know I could actually <em>do</em> that in JavaScript!). I can&#8217;t see why I can&#8217;t just say:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  img.<span style="color: #660066;">onmouseover</span> <span style="color: #339933;">=</span> pno.<span style="color: #660066;">handleMouseOver</span><span style="color: #339933;">;</span>
  img.<span style="color: #660066;">onmouseout</span>  <span style="color: #339933;">=</span> pno.<span style="color: #660066;">handleMouseOut</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The onmouseover and onmouseout need to be assigned a function. But if I give them the foreign object function directly, then something later gets confused. Whereas if I give it an anonymous function that just calls that other function, everything works just fine.</p>
<p>I don&#8217;t know if I&#8217;m doing something wrong. Or if there&#8217;s some strange JavaScript language issues I don&#8217;t know about yet. Or what.</p>
<p>Anyone any suggestions?</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2003/01/object-oriented-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Damned Javascript Links</title>
		<link>http://nothing.tmtm.com/2002/06/damned-javascript-links/</link>
		<comments>http://nothing.tmtm.com/2002/06/damned-javascript-links/#comments</comments>
		<pubDate>Thu, 27 Jun 2002 08:20:22 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=354</guid>
		<description><![CDATA[Day 13 of Mark&#8217;s Accessibility Tips is &#8220;Include Real Links&#8221;. This is a personal bugbear, but not for any of the reasons Mark lists. Me, I hate javascript links because I use Mozilla with my middle mouse button bound to &#8220;open link in new tab&#8221;. Which, of course, only works with real links. JavaScript links [...]]]></description>
			<content:encoded><![CDATA[<p>Day 13 of Mark&#8217;s Accessibility Tips is &#8220;Include Real Links&#8221;.</p>
<p>This is a personal bugbear, but not for any of the reasons Mark lists. Me, I hate javascript links because I use Mozilla with my middle mouse button bound to &#8220;open link in new tab&#8221;. Which, of course, only works with real links. JavaScript links don&#8217;t want to open in a new tab for me, they usually want to spawn a pop-up window. But I don&#8217;t want lots of windows. That&#8217;s why I switched to Mozilla. Now I can still have lots of pages open at once without my windows explorer bar being completely unreadable.</p>
<p>Unless you decide that what I really want is popups.</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2002/06/damned-javascript-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Drag and Drop</title>
		<link>http://nothing.tmtm.com/2002/04/more-drag-and-drop/</link>
		<comments>http://nothing.tmtm.com/2002/04/more-drag-and-drop/#comments</comments>
		<pubDate>Tue, 16 Apr 2002 13:54:01 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=215</guid>
		<description><![CDATA[Scott Andrew has a nice drag and drop window demonstration. Unfortunately there&#8217;s a nasty bug where Mozilla can&#8217;t drop the box, but the concept is good. There&#8217;s lots of little loose ends that he&#8217;s tossing out for other people to fill in, which is good too. This is already very neat, but it could be [...]]]></description>
			<content:encoded><![CDATA[<p>Scott Andrew has a nice <a href="http://www.scottandrew.com/weblog/articles/domwin_1">drag and drop window</a> demonstration. Unfortunately there&#8217;s a nasty bug where Mozilla can&#8217;t drop the box, but the concept is good. There&#8217;s lots of little loose ends that he&#8217;s tossing out for other people to fill in, which is good too. This is already very neat, but it could be exceptional when it gets fleshed out a bit more.</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2002/04/more-drag-and-drop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drag and Drop redux</title>
		<link>http://nothing.tmtm.com/2002/04/drag-and-drop-redux/</link>
		<comments>http://nothing.tmtm.com/2002/04/drag-and-drop-redux/#comments</comments>
		<pubDate>Sat, 06 Apr 2002 19:37:04 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=203</guid>
		<description><![CDATA[It seems that each of the various drag and drop event handlers only know about either the object being dragged, or the area into which it&#8217;s being dropped. Not both. This means that when dropping an object its receiver can&#8217;t know what&#8217;s arriving. There is a dataTransfer object that can be seen by both ends, [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that each of the various drag and drop event handlers only know about either the object being dragged, or the area into which it&#8217;s being dropped. Not both. This means that when dropping an object its receiver can&#8217;t know what&#8217;s arriving.</p>
<p>There is a <tt>dataTransfer</tt> object that <em>can</em> be seen by both ends, which acts as a clipboard onto which you can paste relevant information: but seemingly only either text or a URL (presumably for dragging text or images respectively) &#8211; not the object itself.</p>
<p>This means I&#8217;ve had to resort to the terrible hack:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> makeMovable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    event.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Text&quot;</span><span style="color: #339933;">,</span> event.<span style="color: #660066;">srcElement</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    event.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;URL&quot;</span><span style="color: #339933;">,</span>  <span style="color: #3366CC;">&quot;http://ok/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> dropItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> isMovable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    ...
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> isMovable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;URL&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;http://ok/&quot;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">&amp;&amp;</span> event.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Text&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>[I tried just setting both and making sure both were set, but text dragged from an href has both set already :( ]</p>
<p>This can&#8217;t be the best way to do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2002/04/drag-and-drop-redux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Drag and Drop</title>
		<link>http://nothing.tmtm.com/2002/04/javascript-drag-and-drop/</link>
		<comments>http://nothing.tmtm.com/2002/04/javascript-drag-and-drop/#comments</comments>
		<pubDate>Sat, 06 Apr 2002 16:22:44 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=202</guid>
		<description><![CDATA[Spend a few hours this afternoon playing with drag and drop in IE. There seems to be two main approaches. At JavaScriptKit they use onmousedown and onemouseup to grab elements with class=draggable and then dynamically change their x/y position. This creates a better visual effect than the official Microsoft approach, which uses a variety of [...]]]></description>
			<content:encoded><![CDATA[<p>Spend a few hours this afternoon playing with drag and drop in IE.</p>
<p>There seems to be two main approaches. At <a href="http://www.javascriptkit.com/howto/drag.shtml">JavaScriptKit</a> they use onmousedown and onemouseup to grab elements with class=draggable and then dynamically change their x/y position.</p>
<p>This creates a better visual effect than the <a href="http://msdn.microsoft.com/workshop/author/datatransfer/overview.asp">official Microsoft approach</a>, which uses a variety of new eventhandlers. But the Microsoft way allows for more powerful scripting, as you get to call functions not just when you grab/drag/drop, but also as enter or leave a &#8216;drop-zone&#8217;.</p>
<p>I can&#8217;t decide yet which I prefer. I should probably look at ways to combine both.</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2002/04/javascript-drag-and-drop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

