<?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>dominykas.com &#187; javascript</title>
	<atom:link href="http://www.dominykas.com/tag/javascript.rss2.xml" rel="self" type="application/rss+xml" />
	<link>http://www.dominykas.com</link>
	<description>I&#039;d like to someday write a book about asking the right questions</description>
	<lastBuildDate>Sun, 14 Aug 2011 15:26:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>DublinJS August meetup kata</title>
		<link>http://www.dominykas.com/2011/08/dublinjs-august-meetup-kata.html</link>
		<comments>http://www.dominykas.com/2011/08/dublinjs-august-meetup-kata.html#comments</comments>
		<pubDate>Sun, 14 Aug 2011 15:26:03 +0000</pubDate>
		<dc:creator>Dominykas</dc:creator>
				<category><![CDATA[Webbie stuff]]></category>
		<category><![CDATA[dublinjs]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.dominykas.com/?p=141</guid>
		<description><![CDATA[I&#8217;m doing a kata for this month&#8217;s meetup of Javascript Dublin group and my choice is Roman Numerals. It&#8217;s simple enough and it also demos some interesting transformations of the code during the whole TDD process (if you stick to the mantra of &#8220;write the simplest possible thing&#8221;). I found that I could also put [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m doing a kata for <a href="http://www.meetup.com/Javascript-Dublin/events/27369271/">this month&#8217;s</a> meetup of <a href="https://groups.google.com/group/dublinjs">Javascript Dublin</a> group and my choice is <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataRomanNumerals"><strong>Roman Numerals</strong></a>. It&#8217;s simple enough and it also demos some interesting transformations of the code during the whole TDD process (if you stick to the mantra of &#8220;write the simplest possible thing&#8221;). I found that I could also put some of the new ES5 features into practice: <code>Object.keys()</code>, <code>Array.prototype.forEach()</code> and <code>Array.prototype.reduce()</code>.</p>
<p>The Coding Dojo page for this kata also has a <a href="http://vimeo.com/15104374">video of it performed in Ruby</a>. There&#8217;s also a link to a video of it in Excel, but as much as it sounds good, it&#8217;s just building a VB macro&#8230;</p>
<p>You can find <a href="https://github.com/dymonaz/dublinjs">my code on GitHub</a>. I have also prepared a <a href="https://github.com/dymonaz/dublinjs/blob/master/roman_numbers.walkthrough.txt">walk through</a> on how I arrived at <a href="https://github.com/dymonaz/dublinjs/tree/master/20110814.roman_numbers">my solution</a> (txt is the new ppt!)</p>
<p>The previous kata sessions used Jasmine &#8211; and it works just fine &#8211; but I chose to do things with YUI Test. If you feel like it &#8211; I prepared a <a href="https://github.com/dymonaz/dublinjs/tree/master/yui-test-harness">skeleton for the project</a>. Note that it loads YUI from Yahoo&#8217;s CDN, so make sure you&#8217;re online (or update the harness.html).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dominykas.com/2011/08/dublinjs-august-meetup-kata.rss2.xml</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Opera Extensions: messaging example</title>
		<link>http://www.dominykas.com/2010/11/opera-extensions-messaging-example.html</link>
		<comments>http://www.dominykas.com/2010/11/opera-extensions-messaging-example.html#comments</comments>
		<pubDate>Thu, 04 Nov 2010 01:08:33 +0000</pubDate>
		<dc:creator>Dominykas</dc:creator>
				<category><![CDATA[Webbie stuff]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[opera]]></category>

		<guid isPermaLink="false">http://www.dominykas.com/?p=117</guid>
		<description><![CDATA[I already vented out my frustration with examples that have flaws, so this one is more constructive and tries to actually explain and solve things. You&#8217;re welcome to skip all of that and just get the example code.
It may not be 100% correct, it may have it&#8217;s own flaws, and obviously it may soon become [...]]]></description>
			<content:encoded><![CDATA[<p>I already <a href="http://twitter.com/dymonaz/status/29608503399">vented out</a> my <a href="http://dev.opera.com/forums/topic/793632">frustration with examples that have flaws</a>, so this one is more constructive and tries to actually explain and solve things. You&#8217;re welcome to skip all of that and just <a href="http://code.dominykas.com/opera-extensions/title_in_popup.oex">get the example code</a>.</p>
<p>It may not be 100% correct, it may have it&#8217;s own flaws, and obviously it may soon become obsolete, as Opera 11 is still in alpha, but I did create a more reliable example of how to communicate between the user scripts and popup windows.</p>
<p>This article assumes you already grok the <a href="http://dev.opera.com/articles/view/getting-started-with-opera-extensions/">introductory information on Opera extension</a>. In fact, knowledge of Chrome extension flows may also help.</p>
<h2>The flaws</h2>
<p>Let&#8217;s skip the ranting about lack of documentation (hint hint) and look at the fundamental flaws in the original <a href="http://dev.opera.com/articles/view/opera-extensions-messaging/#popup_injectedscript">Communicating between popup and injected script</a> example. You can download <a href="http://code.dominykas.com/opera-extensions/message_example3/message_example3_modified.oex">the modified version of it</a> that attempts to display the page title. I also documented the steps to <a href="http://code.dominykas.com/opera-extensions/message_example3/readme.txt">reproduce two bugs</a> in it.</p>
<ol>
<li>There is only one communication channel &#8211; and it&#8217;s always reset to the page which was loaded last.</li>
<li>The above means, that we may not be communicating with the focused tab.</li>
<li>The above also means that the channel is not re-established once a new popup opens &#8211; even if it&#8217;s on the same tab. I suspect that <code>if (port)</code> is supposed to check whether the channel is still active, but (potentially &#8211; due to a browser bug) it returns a truthy value, hence the new channel is not sent to the popup.</li>
<li>The check inside <code>onconnect</code> only validates that a communication channel was created &#8211; it never validates that the connecting script is actually a popup window</li>
</ol>
<h2>The solution</h2>
<p>In here, I have a couple of things that are merely assumptions on how things work, and how the API should respond &#8211; this is mostly due to lack of documentation (hint hint, again) and is based on experience with Chrome extensions.</p>
<p>The core point of the solution is to <strong>avoid storing the communication channel in the background script</strong> &#8211; instead &#8211; store a reference to the popup window. I feel that it is safe to do, since the popup window closes automatically, and it seems that all data associated with it gets destroyed.</p>
<p>The background script still plays an important role &#8211; it&#8217;s job is to pass the <code>MessagePort</code> from the user script to the popup, but it only does that when requested. As mentioned above, when the popup window closes, we also need to re-establish the connection, so the final flow becomes like this:</p>
<ol>
<li>Browser loads, background script is initialized</li>
<li>A page in a tab loads, user script gets initialized</li>
<li>User clicks on the toolbar icon for the popup</li>
<li>The popup loads</li>
<li>Background page receives an <code>onconnect</code> event</li>
<li>It then checks the event origin &#8211; all widget related files have URLs that start with <code>widget://</code></li>
<li>If the thing that just connected is a popup window, the background script determines the active tab, and sends a <code>getPorts</code> message to initiate messaging connection. It also stores the reference to the popup for future communication.</li>
<li>The user script receives the message and creates a new communication channel (it assumes that the previous channel is long dead)</li>
<li>It then sends that communication channel back to the background script using <code>establishConnection</code> message.</li>
<li>The background script forwards that message to the previously stored popup reference. I feel that it is safe enough to trust that, since the steps 5-9 should really be pretty much instantaneous without any user interaction possible in between.</li>
<li>The popup receives the communication channel &#8211; it is free to do with it whatever it likes, e.g. request information from the user script using <code>postMessage()</code> and receive that information using <code>onmessage</code>.
<li>Once the popup closes, we end up at point 2</li>
</ol>
<p><a href="http://code.dominykas.com/opera-extensions/title_in_popup.oex">Download example code</a> (I release it into public domain, without any guarantees)</p>
<h2>The gotchas, suggestions and pleas to the Opera team</h2>
<h3>Document <code>MessageChannel</code> and <code>MessagePort</code></h3>
<p>While this is an obvious request, I&#8217;d also like to mention that I find Chrome&#8217;s model of two way messaging much simpler &#8211; just pass around a callback function for each message.</p>
<h3>Rename <code>port1</code> and <code>port2</code> to something meaningful</h3>
<p>My initial response to these two properties was that one of them is for sending messages, the other is for receiving. As far as I understand that right now &#8211; one is to be used as a &#8220;local&#8221; communication port, and the other is for the use by &#8220;remote&#8221; script. They should be named accordingly. However, a much better approach would be to not expose them at all &#8211; same as the previous suggestion &#8211; it should be possible to just create a <code>MessageChannel</code> with <code>postMessage()</code> and <code>onmessage</code> &#8211; which are inversed once they change context.</p>
<h3>Clarify if objects are allowed as <code>event.data</code></h3>
<p>I&#8217;ll admit &#8211; I didn&#8217;t read the W3C spec. My example uses simple objects, but the <a href="http://labs.opera.com/extensions-api/">API documentation</a> says the message data has to be a <code>DOMString</code>. While it&#8217;s not a biggie, and I&#8217;m sure everyone is good friends with <code>JSON.stringify()</code> and <code>JSON.parse()</code>, it either should be documented as allowed or should throw errors when used.</p>
<h3>Create an easier way to determine who is the <code>event.origin</code></h3>
<p>At the moment, to check if the message was sent by a popup, I&#8217;m testing the URL in the <code>event.origin</code>, but I&#8217;d really like to see some methods, such as <code>isBackgroundScript()</code>, <code>isUserScript()</code> and <code>isPopup()</code> for simplicity.</p>
<h3>More graphical information</h3>
<p>I&#8217;m guilty here, since I probably should have included the flow diagram instead of an 11 point list in this blog, but I do find the <a href="http://labs.opera.com/extensions-api/visual-guide/index.htm">visual guide to the API</a> very useful. The <a href="http://dev.opera.com/">Dev.Opera</a> article about messaging inside extensions should have included some of that love.</p>
<h3>Explain best practices</h3>
<p>Having looked through most of the example extensions, I find certain things quite interesting &#8211; and they do raise a lot of &#8220;why&#8221; type questions:</p>
<ul>
<li>Why do we need to do stuff on <code>load</code> in the background scripts?</li>
<li>How does that make a difference as compared to just executing the code in the <code>&lt;script&gt;</code> tag?</li>
<li>Do we also have to do things on <code>load</code> in the popup scripts?</li>
<li>Why do some examples check for existence of <code>opera.extension</code>? When it might not be available?</li>
<li>What are the &#8220;dos&#8221; and &#8220;donts&#8221; for the background process? What are the limitations? Considering that it&#8217;s a page that&#8217;s always active, I&#8217;m sure there are plenty.</li>
</ul>
<p>I hope the time spent with frustration, figuring things out and writing up this article was worth it. I also hope that it&#8217;s useful for at least one other person. However, in case of the information in here is already published and the questions are already answered &#8211; please &#8211; drop me a link in the comments or <a href="http://twitter.com/dymonaz">on twitter @dymonaz</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dominykas.com/2010/11/opera-extensions-messaging-example.rss2.xml</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmark: HTML outlining/TOC scripts</title>
		<link>http://www.dominykas.com/2010/01/benchmark-html-outlining-toc-scripts.html</link>
		<comments>http://www.dominykas.com/2010/01/benchmark-html-outlining-toc-scripts.html#comments</comments>
		<pubDate>Mon, 18 Jan 2010 22:09:12 +0000</pubDate>
		<dc:creator>Dominykas</dc:creator>
				<category><![CDATA[Webbie stuff]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.dominykas.com/?p=68</guid>
		<description><![CDATA[A bit over a week ago Chris Heilmann published a post on table of contents generators, where he listed a couple of JavaScript methods to do that. It&#8217;s been always in the plans to optimize the performance of my own implementation of the HTML5 outlining routine &#8211; and what better way to measure it, than [...]]]></description>
			<content:encoded><![CDATA[<p>A bit over a week ago <a href="http://www.wait-till-i.com/2010/01/06/the-table-of-contents-script-my-old-nemesis/">Chris Heilmann published a post on table of contents generators</a>, where he listed a couple of JavaScript methods to do that. It&#8217;s been always in the plans to optimize the performance of <a href="http://code.google.com/p/h5o/">my own implementation of the HTML5 outlining routine</a> &#8211; and what better way to measure it, than to compare it with some other approaches while I&#8217;m at it. To no surprise, I lost. Big time.</p>
<h2>Results</h2>
<ol>
<li>Chris Heilmann&#8217;s <a href="http://isithackday.com/demos/tocit/toc_yui3.html">YUI based approach</a> (136ms)</li>
<li>Chris Heilmann&#8217;s <a href="http://isithackday.com/demos/tocit/toc_dom.html">DOM based approach</a> (247ms)</li>
<li>Stuart Langridge&#8217;s <a href="http://www.kryogenix.org/code/browser/generated-toc/">generated-toc</a> (258ms)</li>
<li><a href="http://github.com/joshwnj/toc">Josh Johnston&#8217;s solution</a> with jQuery (781ms)</li>
<li>My own <a href="http://code.google.com/p/h5o/">HTML5 Outliner</a> (3099ms)</li>
<li>Chris Heilmann&#8217;s <a href="http://isithackday.com/demos/tocit/toc_js.html">RegExp based approach</a> (4440ms)</li>
<li><a href="http://lab.diogovincenzi.com/blog/view/table-contents-jquery">Diogo Vincenzi&#8217;s solution</a> with jQuery (9334ms)</li>
</ol>
<h3>Methodology</h3>
<p>The numbers above are based on an 1.5Mb file, which has some 200 headings to use for the TOC table. The actual file, was created out of HTML4 specification part on forms, to reflect a more natural usage. The results were a bit different, when I used my own generated file, which had a lot more headings, and a lot less text.</p>
<p>The test runner itself would create an <code>iframe</code>, load the test document inside, inject the required script and call it. The <a href="http://code.google.com/p/h5o/source/browse/trunk/benchmark">source is available</a> on Google Code (though be aware, that you will need to procure and hack the dependencies yourself).</p>
<p>I ran it on a decent 2.2Ghz laptop, using a vanilla version of Portable Firefox 3.5. With some exceptions (see below), the results were comparable and not unexpected in other browsers.</p>
<h2>Insights</h2>
<ul>
<li>The RegExp based approach was a non-contender from the start, as it doesn&#8217;t really work under any browser, other than Firefox.</li>
<li>Diogio&#8217;s solution scored so low because it&#8217;s querying the DOM twice inside every loop iteration (I think).</li>
<li>I am impressed at how well YUI3 performed &#8211; on every browser (incl. IE8), except Opera. I seriously need to follow up on the Opera thing&#8230;</li>
<li>Speaking of YUI3 &#8211; one needs to go through extra hoops to measure performance, due to YUI loader&#8217;s <code>setTimeout()</code>. And make sure that you have all dependencies in on file, to avoid calls to the Yahoo&#8217;s CDN.</li>
<li>IE8 didn&#8217;t like 2Mb documents inside an <code>iframe</code> (hence 1.5Mb)</li>
<li>I have plenty of space for improvement &#8211; it will be fun!</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.dominykas.com/2010/01/benchmark-html-outlining-toc-scripts.rss2.xml</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>javascript:void(0) must die</title>
		<link>http://www.dominykas.com/2009/12/javascript-void-must-die.html</link>
		<comments>http://www.dominykas.com/2009/12/javascript-void-must-die.html#comments</comments>
		<pubDate>Wed, 02 Dec 2009 23:32:31 +0000</pubDate>
		<dc:creator>Dominykas</dc:creator>
				<category><![CDATA[Webbie stuff]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.dominykas.com/?p=8</guid>
		<description><![CDATA[Pretty much two weeks ago I dropped my notes from FullFrontal 2009 onto developer&#8217;s forum at work. And one full colleague of mine enquired why did I say that &#8220;javascript:void(0) must die&#8221;. I didn&#8217;t really have a chance to follow it up until now (happy birthday, dear blog).
It doesn&#8217;t do anything, it just sits there!
Now, [...]]]></description>
			<content:encoded><![CDATA[<p>Pretty much two weeks ago I dropped my notes from <a href="http://2009.full-frontal.org/">FullFrontal 2009</a> onto developer&#8217;s forum at work. And one full colleague of mine enquired why did I say that &#8220;<code>javascript:void(0)</code> must die&#8221;. I didn&#8217;t really have a chance to follow it up until now (happy birthday, dear blog).</p>
<h2>It doesn&#8217;t do anything, <a title="Video: Electric Six - Broken Machine" href="http://www.youtube.com/watch?v=pjq3vQV6v-Y">it just sits there</a>!</h2>
<p>Now, normally I&#8217;m not a complete standards freak (unlike the guys working on <a href="http://code.google.com/p/google-caja/">Caja</a>). But what does <code>&lt;a href="javascript:void(0)"&gt;</code> really mean? The answer is simple &#8211; <strong>nothing</strong>.</p>
<p>First of, it&#8217;s a hyperlink to a resource, which should be accessed over Javascript &#8220;protocol&#8221;. Here&#8217;s the problem &#8211; I don&#8217;t really feel that &#8220;javascript&#8221; is a protocol. And it can&#8217;t really handle resources. But lets ignore that. What is the actual resource?</p>
<p>The link tells &#8220;use javascript&#8221; to &#8220;retrieve&#8221;&#8230; <strong>void</strong>. Why would anyone in the world want to view void in their browser? If anyone ever wants to look at it &#8211; they&#8217;re much better off achieving that goal on YouTube&#8230;</p>
<h2>Semantics apart &#8211; it&#8217;s not comprehensible</h2>
<p>Lets suppose, you don&#8217;t really care about web standards and semantics. I&#8217;d probably agree with you &#8211; we&#8217;re quite far away from having a meaningful web (if ever). Still, having crap as the URL is no good &#8211; apparently, <strong>screen readers will read that URL</strong> out loud.</p>
<p>Can you imagine anyone discussing this over a pint: &#8220;You know, I found this great site at <i>javascript colon void opening parenthesis zero closing parenthesis</i>&#8220;? That kind of URL would not make any sense to regular folks.</p>
<h2>Accessibility is NOT about screen readers, though</h2>
<p>Such a link kills browsing experience for sighted users too. Have you ever tried what happens when you <strong>middle-click the void link</strong>? Or right-click and then select &#8220;Open in new tab&#8221;? Here &#8211; <a href="javascript:void(0)" onclick="alert('Firefox and Opera open a background tab, Chrome and IE8 execute onclick JS upon middle click');">try it out</a> (mileage may vary if you use a recent browser).</p>
<p>Exactly &#8211; you guessed it &#8211; <strong>nothing</strong> meaningful happens. Just as it should!</p>
<h2>Solution 1: Use real URLs</h2>
<p>There are several ways out of this. My favorite one is to provide a real URL. If you&#8217;re following progressive enhancement methodology in your work, you always want to provide alternative links to achieve (near full) functionality even when Javascript is disabled.</p>
<p>This means, that instead of having a <code>&lt;a href="javascript:void(0)"&gt;Delete&lt;/a&gt;</code> and attaching <code>onclick</code> handler to it, popping out a confirm dialog and then making a background call using XHR, you provide a link to e.g. <code>/myResource?delete</code> which brings up a form, with a real <code>&lt;button&gt;</code>.</p>
<p>The actual backend code doesn&#8217;t become that much trickier &#8211; you don&#8217;t really have to write up a new &#8220;delete&#8221; routine. By using a confirmation interstitial, you also provide a way to confirm the action, and you can still use a POST request, as this action has permanent effect.</p>
<h2>Solution 2: don&#8217;t use anchor tag</h2>
<p>The above solution doesn&#8217;t always work &#8211; if you have a really interactive application, you&#8217;re not always able to provide links. However, since you&#8217;re already using JS &#8211; why use the <code>&lt;a&gt;</code> tag where it doesn&#8217;t belong?</p>
<p>I did hear some advice, to use <code>&lt;button&gt;</code> for clickable things, which are not really links. You should investigate that, but anyone who has tried to style buttons, knows that it can very easily become a major pain.</p>
<p>Still, you can attach your <code>onclick</code> handlers on anything you want &#8211; <code>div</code>, <code>span</code> and so on. In such cases, you should also add <code>tabindex</code> to make elements focusable and make use of ARIA roles to provide guidelines on behaviour. It&#8217;s not really that hard, now, is it?</p>
<p>The only really painful thing here is providing <code>:hover</code> and <code>:focus</code> styles on IE6. But IE6 should also die, together with <code>javascript:void(0)</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dominykas.com/2009/12/javascript-void-must-die.rss2.xml</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

