<?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>James Gregory &#187; JavaScript</title>
	<atom:link href="http://jagregory.com/writings/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://jagregory.com</link>
	<description>Monkeying with the code</description>
	<lastBuildDate>Tue, 22 Jun 2010 11:07:25 +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>A usability moan about Adobe&#8217;s website</title>
		<link>http://jagregory.com/writings/a-usability-moan-about-adobes-website/</link>
		<comments>http://jagregory.com/writings/a-usability-moan-about-adobes-website/#comments</comments>
		<pubDate>Sat, 01 Jul 2006 10:22:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=5</guid>
		<description><![CDATA[When recently performing a system-wide clean-out, I removed a long-expired Macromedia Flash 8 Studio trial. Unknown to me at the time, it had taken it upon itself to remove the flash player as-well; because, of course, now that I don’t want to develop flash, I’ll never need it again!Anyway, a week or so later I [...]]]></description>
			<content:encoded><![CDATA[<p>When recently performing a system-wide clean-out, I removed a long-expired Macromedia Flash 8 Studio trial. Unknown to me at the time, it had taken it upon itself to remove the flash player as-well; because, of course, now that I don’t want to develop flash, I’ll never need it again!Anyway, a week or so later I discovered that I needed to re-install the player, so off I went to <a href="http://www.adobe.com">Adobe’s Website</a>. When I arrived, I was presented with a fairly reasonable page &#8211; in terms of design &#8211; but there was something on there that epitomises the lack of thought that goes into usability on the web. People, you have to remember it isn’t just about how you create your links (bad “click here”, no play-time for you!), it’s also about how your content is worded.<img src="/wp-content/uploads/2007/07/adobe-flash-small.jpg" style="float: right" alt="Adobe's no-flash warning message" />What you can see in the screenshot is what you’re shown if you don’t have the flash player installed (or JavaScript enabled, I’m guessing).</p>
<p>What’s wrong with this picture?</p>
<p>It’s very misleading, that’s what.</p>
<p>The heading is asking me “Can’t see this content?”, but I can, can’t I? I’m reading it! Oh, the word “this” isn’t associated to what I’m reading (as it should be), but rather what I’m <strong>not</strong> reading. Interesting.</p>
<p>Misleading text has also found its way into the body content, once again, I am reading the content aren’t I? Another thing is that the content also implies that I have JavaScript disabled when I actually don’t. I believe you’re statistically more likely to have a user without flash installed, than without JavaScript enabled. So with that in mind, it would have been a better idea to ask if they have flash first then ask about JavaScript. Even better would be to do a check to see if JavaScript is enabled, if it is you don’t need to display that part of the message at all!</p>
<p>One last thing, those controls at the bottom; what are they doing there? I know (now) that they’re there because the content that’s supposed to be showing is a movie, but prior to having that knowledge surely they just reinforce the feeling that I am seeing what I’m supposed to. I have content, I have play controls, hey why aren’t they working? They serve no purpose apart from to confuse the user.</p>
<h3>A slightly better picture</h3>
<p>I’ve modified the content to be &#8211; in my eyes &#8211; a bit more respectable. It still isn’t perfect, like I said above, some JavaScript detection would be nice. Here it is non-the-less though:</p>
<p><img src="/wp-content/uploads/2007/07/adobe-flash-fixed.gif" style="float: right" alt="An improved version of Adobe's message" />“Reading this?”</p>
<p>I most certainly am.</p>
<p>“If you are reading this message, then you are unable to view our active content.”</p>
<p>Oh dear, so that’s what this is all about. How do I fix it?</p>
<p>“Please either download the latest version of the Macromedia Flash Player or enable JavaScript in your browser.”</p>
<p>Will do, thanks for the help Mr. Usable Information Box.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/a-usability-moan-about-adobes-website/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Internet Explorer&#8217;s conditional comments for targeted JavaScript</title>
		<link>http://jagregory.com/writings/using-ies-conditional-comments-for-targeted-javascript/</link>
		<comments>http://jagregory.com/writings/using-ies-conditional-comments-for-targeted-javascript/#comments</comments>
		<pubDate>Thu, 15 Jun 2006 12:40:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=9</guid>
		<description><![CDATA[The demise of CSS hacks, something which has been covered elsewhere to no end, definitely a good thing, but not what I’m covering here &#8211; not directly anyway. I’ve been wondering if we can take advantage of this new age of “hack segregation”.
The way I see it is we’re already breaking the rule of separation [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/ie/archive/2005/10/12/480242.aspx">demise of CSS hacks</a>, something which has been covered elsewhere to no end, definitely a good thing, but not what I’m covering here &#8211; not directly anyway. I’ve been wondering if we can take advantage of this new age of “hack segregation”.</p>
<p>The way I see it is we’re already breaking the rule of separation by using conditional comments in the structure, so why don’t we cease this opportunity to save our friends (users of Firefox, Safari, Opera etc&#8230;) some bandwidth by providing IE specific JavaScript in the same way too?</p>
<p>For those wondering what I could mean by “IE specific JavaScript”, an example would be simulating the CSS focus pseudo class on form elements &#8211; something highly recommended for an accessible and usable form &#8211; which aren’t implemented in the market standard versions of Internet Explorer (6 and below).</p>
<p>I don’t think users of compliant browsers should be punished by downloading extraneous scripts, loading events into the queue and then spending time evaluating code that doesn’t even apply to them.</p>
<p>I’m proposing that we devote a section of the page <code>head</code> tag to Internet Explorer, that’s simply a conditional comment section that contains any style sheets and script blocks that only apply to IE.</p>
<h3>Example 1 (what we are doing)</h3>
<pre><code>&lt;!--[if lt IE 7]&gt;</code></pre>
<pre><code>&lt;link rel="stylesheet" type="text/css" href="/css/ie-specific.css" /&gt;</code></pre>
<pre><code>&lt;![endif]--&gt;</code></pre>
<pre><code>&lt;script type="text/javascript" src="/js/ie-specific.js"&gt;&lt;/script&gt;</code></pre>
<pre><code>&lt;script type="text/javascript"&gt;</code></pre>
<pre><code>    addEvent(window, 'load', function() { if (document.all) { doIEStuff(); });</code></pre>
<pre><code>&lt;/script&gt;</code></pre>
<h3>Example 2 (what we could be doing)</h3>
<pre><code>&lt;!--[if lt IE 7]&gt;</code></pre>
<pre><code>&lt;link rel="stylesheet" type="text/css" href="/css/ie-specific.css" /&gt;</code></pre>
<pre><code>&lt;script type="text/javascript" src="/js/ie-specific.js"&gt;&lt;/script&gt;</code></pre>
<pre><code>&lt;script type="text/javascript"&gt;</code></pre>
<pre><code>    addEvent(window, 'load', doIEStuff);</code></pre>
<pre><code>&lt;/script&gt;</code></pre>
<pre><code>&lt;![endif]--&gt;</code></pre>
<p>In <em>Example 1</em> only Internet Explorer (prior to version 7) will download the stylesheet, but all browsers download the “ie-specific.js” JavaScript file and evaluate the <code>addEvent</code> code block. However if we follow <em>Example 2</em>, Internet Explorer will download the JavaScript and CSS just like normal, but other browsers simply see the whole section as a standard html comment and thus won’t download anything or evaluate the code block.</p>
<p>Wonderful, eh? I feel more comfortable with this approach than using questionable browser detection methods in JavaScript and it also reinforces (by just a tiny little bit) that compliant browsers are faster than their non-compliant brethren.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/using-ies-conditional-comments-for-targeted-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript Console in Safari</title>
		<link>http://jagregory.com/writings/javascript-console-in-safari/</link>
		<comments>http://jagregory.com/writings/javascript-console-in-safari/#comments</comments>
		<pubDate>Mon, 15 May 2006 14:31:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Nuggets]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=12</guid>
		<description><![CDATA[Just a quick tip to enable the Debug menu in Safari, the main reason being to get the JavaScript Console (ala Firefox).
Simply close down your open instances of Safari and enter this into the Terminal:
defaults write com.apple.Safari IncludeDebugMenu 1
That’ll enable a new Debug menu next time you start Safari, simple as that. I was on [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick tip to enable the Debug menu in Safari, the main reason being to get the JavaScript Console (ala Firefox).</p>
<p>Simply close down your open instances of Safari and enter this into the Terminal:</p>
<pre><code>defaults write com.apple.Safari IncludeDebugMenu 1</code></pre>
<p>That’ll enable a new Debug menu next time you start Safari, simple as that. I was on the verge of loading up Firefox on the Mac<br />
before I came across this, bad James!</p>
<p>Also, just in-case, running the above again with 0 will turn off the menu.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/javascript-console-in-safari/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ServerControlIDs Update</title>
		<link>http://jagregory.com/writings/servercontrolids-update/</link>
		<comments>http://jagregory.com/writings/servercontrolids-update/#comments</comments>
		<pubDate>Wed, 26 Apr 2006 09:20:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=14</guid>
		<description><![CDATA[I’ve created a small update for my ServerControlIDs control, which removes the problem caused by controls within Repeaters and DataGrid.
If you had a repeater that for each item contained an Hyperlink control, you’d get a duplicate entry in the ClientIDs object for every record, to avoid this problem I simply don’t output any sub-controls from [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve created a small update for my ServerControlIDs control, which removes the problem caused by controls within Repeaters and DataGrid.</p>
<p>If you had a repeater that for each item contained an Hyperlink control, you’d get a duplicate entry in the ClientIDs object for every record, to avoid this problem I simply don’t output any sub-controls from within repeater style objects.</p>
<p>That may sound like a bit of a hack, maybe it is, but if you want to refer to multiple elements within another, you shouldn’t really be doing it by ID anyway.</p>
<p>You can get the <a href="http://www.jagregory.com/downloads/ServerControlIDs-1.1.zip">updated source here</a> or view the <a href="/pages/ServerControlIDs" title="ASP.Net ClientIDs and their JavaScript companion">full article here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/servercontrolids-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How-to use ClientIDs in JavaScript without the ugliness</title>
		<link>http://jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/</link>
		<comments>http://jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/#comments</comments>
		<pubDate>Wed, 12 Apr 2006 07:34:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=16</guid>
		<description><![CDATA[ASP.Net has an interesting way of handing the potential ID conflicts caused by embedding third-party controls within your web-page; it prefixes any sub-controls with their parent’s ID.
&#60;asp:TextBox ID="txtUsername" Runat="server" /&#62; within a standard page simply has an ID of txtUsername within the HTML output. On the other hand, the following example would result in something [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.Net has an interesting way of handing the potential ID conflicts caused by embedding third-party controls within your web-page; it prefixes any sub-controls with their parent’s ID.</p>
<p><code>&lt;asp:TextBox ID="txtUsername" Runat="server" /&gt;</code> within a standard page simply has an ID of <code>txtUsername</code> within the HTML output. On the other hand, the following example would result in something along the lines of <code>_parent_txtUsername</code> as the ID:</p>
<p><code>&lt;div id="parent" runat="server"&gt;<br />
&nbsp;&nbsp;&lt;asp:TextBox ID="txtUsername" Runat="server" /&gt;<br />
&lt;/div&gt;</code></p>
<p>This isn’t really much of a problem when all you are working with is server-side code, but when you start tinkering with JavaScript, things become quite annoying and get an overall feeling of hackyness.</p>
<p>One solution, which I have used time-and-again, is to use a script block at the end of your page where you create a series of variables that contain the actual IDs. You then use these variables to reach the actual elements via JavaScript.</p>
<p><code>&lt;script type="text/javascript"&gt;<br />
&nbsp;&nbsp;var txtUsernameID = '&lt;%= txtUsername.ClientID %&gt;';<br />
&nbsp;&nbsp;var txtPasswordID = '&lt;%= txtPassword.ClientID %&gt;';<br />
&lt;/script&gt;</code></p>
<p>This solution works fine, but it isn’t exactly pretty and it doesn’t weigh well on my conscience.</p>
<h2>Javascript Mapping</h2>
<p>I’ve created a simple class that traverses the control structure and outputs an object that contains the mappings, as above, but without any client-side intervention; not a <code>&lt;%= ...ClientID %&gt;</code> in sight.</p>
<p>An example of the output would be:<br />
<code>var ClientIDs = {<br />
&nbsp;&nbsp;txtUsername = 'ctrl0_txtUsername',<br />
&nbsp;&nbsp;txtPassword = 'ctrl0_txtPassword'<br />
};</code></p>
<p>Which enables you to reference elements, that you know are server-controls, by calling <code>ClientIDs.txtUsername</code>.</p>
<h3>Before:</h3>
<p><code>&lt;script type="text/javascript"&gt;<br />
&nbsp;&nbsp;var txtUsernameID = '&lt;%= txtUsername.ClientID %&gt;';<br />
&nbsp;&nbsp;var txtPasswordID = '&lt;%= txtPassword.ClientID %&gt;';</code><br />
<br />
<code>&nbsp;&nbsp;function validate() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;var username = $(txtUsernameID).value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;var password = $(txtPasswordID).value;</code><br />
<br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;if (username == 'james') {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("you shouldn't be here!");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&lt;/script&gt;</code></p>
<h3>After:</h3>
<p><code>&lt;script type="text/javascript"&gt;<br />
&nbsp;&nbsp;function validate() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;var username = $(ClientIDs.txtUsername).value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;var password = $(ClientIDs.txtPassword).value;</code><br />
<br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;if (username == 'james') {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("you shouldn't be here!");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&lt;/script&gt;</code></p>
<h2>ServerControlIDs class</h2>
<h3>Methods</h3>
<p><code>void Emit(page)</code> &#8211; Builds the controls of the page into a mapped structure, registering a client-side script block for the output.</p>
<p><code>void Emit(control, literal)</code> &#8211; Builds the controls of the control into a mapped structure, rendering the output into the literal control supplied. The literal is best suited to being placed into the <code>&lt;head&gt;</code> tag, which helps improve the semantic nature of your page.</p>
<h3>Properties</h3>
<p><code>bool Whitespace</code> &#8211; Sets whether the output should contain whitespace, provided only for “prettifying” your code; defaults to off.</p>
<p><code>string VariableName</code> &#8211; Sets the JavaScript variable name used; defaults to ClientID.</p>
<h3>Usage</h3>
<p>To use this class, simply create an instance in the PreRender event for your page — this ensures that all the controls have been set to their correct state — then call the Emit method you desire.</p>
<h3>History</h3>
<p>[1.1] Removed IDs from Repeater/DataGrid contained controls.<br />
[1.0] Initial release</p>
<h3>Download</h3>
<p>That’s all there is to it, use the link below to get the latest version of ServerControlIDs.</p>
<p>Please note that this code isn’t pretty and it could probably do with some revising, but it works and that’s what’s important to me. I’m open to suggestions and feature requests.</p>
<p><a href="http://www.jagregory.com/downloads/ServerControlIDs-1.1.zip">Current Version (1.1)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>The problem with onload</title>
		<link>http://jagregory.com/writings/the-problem-with-onload/</link>
		<comments>http://jagregory.com/writings/the-problem-with-onload/#comments</comments>
		<pubDate>Wed, 12 Apr 2006 05:29:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=17</guid>
		<description><![CDATA[Handling events from within your page has always been a hit and miss affair, especially for somebody who doesn’t like mixing their code and markup. The way to go with events for a long time was to embed an attribute on the element in question; like onclick="do_something()".
That works fine, but we sure know it isn’t [...]]]></description>
			<content:encoded><![CDATA[<p>Handling events from within your page has always been a hit and miss affair, especially for somebody who doesn’t like mixing their code and markup. The way to go with events for a long time was to embed an attribute on the element in question; like <code>onclick="do_something()"</code>.</p>
<p>That works fine, but we sure know it isn’t semantic. The way forward — greatly popularised by the <a href="http://prototype.conio.net">Prototype</a>, <a href="http://dojotoolkit.org/">Dojo</a> and <a href="http://bennolan.com/behaviour/">Behaviour</a> libraries — is to use DOM compliant event listeners. These fire arbitrary code, in a very similar way to attributes, when an event is raised. Unlike attributes these event listeners can be hidden away in their rightful place, the document header.</p>
<p>Perfect &#8211; clean semantic markup and real event handling.</p>
<p>…or is it? Unfortunately nothing is ever smooth when dealing with standards (it’s hard to promote the underdog when the underdog barely works anyway…).</p>
<h3>Our Problem</h3>
<p>The biggest setback for using event listeners is that nothing will fire until the whole of the body has finished loading, including any images that need to be downloaded. This can cause something similar to the <a href="http://www.bluerobot.com/web/css/fouc.asp" title="Flash of Unstyled Content">FOUC</a>, where a page appears in one state for a moment until the javascript executes; not a pretty sight and generally hard to sell as a browser fault.</p>
<p>If you had the following code in your header tag, the element <code>aTable</code> would be visible until the page has finished loading, at which point it would suddenly dissapear.</p>
<pre><code>&lt;script type="text/javascript"&gt;
    function hideATable() {
        document.getElementById('aTable').style['display'] = 'none';
    }

    Event.observe(window, 'load', hideATable, false);
&lt;/script&gt;
</code></pre>
<p>What we really need is an <code>onBodyRendered</code> event to hook to, which fires as soon as the body has been parsed, irrelevant of whether the content has been loaded.</p>
<h3>The Solution</h3>
<p>The only real solution to this problem, as there isn’t an <code>onBodyRendered</code> event, is to force the browser to parse our javascript before it has finished loading. The method of doing this is to embed a script block just before the end of the body tag containing the functions you wish to execute. The reason this works is because the browser parses any javascript it encounters within the body tag and evaluates it while rendering.</p>
<p>The following code would hide the table before the page has finished rendering; exactly what we want.</p>
<pre><code>&lt;script type="text/javascript"&gt;
    hideATable();
&lt;/script&gt;
</code></pre>
<h3>My additions</h3>
<p>The above method isn’t completely desirable, because it is still mixing code with markup and any code within the footer will not be cached. There isn’t much you can do about that really, but one thing that i’ve taken to doing in my websites is to make sure there is only ever one function call in that footer script block.</p>
<p>My method of doing such a thing is to create a special object that represents all my pages and their individual footer script blocks. You can see below an example of such an object, there’s an wrapper object that allows for further extensions (such as Unload or Validation sections) and then the Load object that contains each pages onload function. Each page then has a single line script block like Pages.Load.Home();</p>
<pre><code>var Pages = {};

Pages.Load = {
    Home: function() {
        highlightSomething();
        hideSomething();
        saveCookies();
    },

    Portfolio: function() {
        hideSomething();
        makeMeLookGood();
    },

    Contact: function() {
        highlightFields();
    }
};
</code></pre>
<p>So the above code is stored in an external js file and loaded in the usual way, thus being cached.</p>
<h4>Before:</h4>
<pre><code>&lt;script type="text/javascript"&gt;
    highlightSomething();
    hideSomething();
    saveCookies();
&lt;/script&gt;
</code></pre>
<h4>After:</h4>
<pre><code>&lt;script type="text/javascript"&gt;
    Pages.Load.Home();
&lt;/script&gt;</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/the-problem-with-onload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Behaviour &amp; getElementsBySelector() mini-fix</title>
		<link>http://jagregory.com/writings/javascript-behaviour-amp-getelementsbyselector-mini-fix/</link>
		<comments>http://jagregory.com/writings/javascript-behaviour-amp-getelementsbyselector-mini-fix/#comments</comments>
		<pubDate>Fri, 24 Mar 2006 08:34:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=21</guid>
		<description><![CDATA[Behaviour, great bit of JavaScript!
While using it lately I came across a small bug in the code it’s based-on, Simon Willison’s getElementsBySelector(), when you use a selector that references an invalid ID paired with a tag name (e.g. body#profile, when #profile doesn’t exist), a script error occurs; yet using #profile alone works fine.
After a little [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://bennolan.com/behaviour/">Behaviour</a>, great bit of JavaScript!</p>
<p>While using it lately I came across a small bug in the code it’s based-on, <a href="http://simon.incutio.com/">Simon Willison</a>’s <a href="http://simon.incutio.com/archive/2003/03/25/getElementsBySelector">getElementsBySelector()</a>, when you use a selector that references an invalid ID paired with a tag name (e.g. <code>body#profile</code>, when <code>#profile</code> doesn’t exist), a script error occurs; yet using <code>#profile</code> alone works fine.</p>
<p>After a little bit of digging I found a solution, the source of the error is line 89:</p>
<pre><code>if (tagName &amp;&amp; element.nodeName.toLowerCase() != tagName) {
</code></pre>
<p>It’s finding the <code>tagName</code> (<code>body</code>), but element is undefined due to the ID being invalid. A slight change to the above if statement fixes the problem.</p>
<pre><code>if (!element || (tagName &amp;&amp; element.nodeName.toLowerCase() != tagName)) {
</code></pre>
<p>Maybe somebody will find that useful, I’ve posted it on Simons blog, but incase he doesn’t accept the change you can download the modified version here.</p>
<p><span class="note"><br />
Note: If you’re using Behaviour, as opposed to just <code>getElementsBySelector()</code>, you’ll need to paste the contents of the above file into the bottom of your behaviour.js file, overwriting everything after the <code>Behaviour.start();</code> line.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/javascript-behaviour-amp-getelementsbyselector-mini-fix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Personal Note: A brief interlude</title>
		<link>http://jagregory.com/writings/just-so-you-know/</link>
		<comments>http://jagregory.com/writings/just-so-you-know/#comments</comments>
		<pubDate>Tue, 07 Mar 2006 07:49:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=25</guid>
		<description><![CDATA[Recently, while working on my website I&#8217;ve become exposed to the Behaviour JavaScript library, which I may say is absolutely wonderful. Very simple and straight forward, completely removes the need for those cursed script tags appended to the bottom of a page; something which I’ve never liked doing but became a bit of a necessary [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, while working on my website I&#8217;ve become exposed to the <a href="http://bennolan.com/behaviour/">Behaviour JavaScript library</a>, which I may say is absolutely wonderful. Very simple and straight forward, completely removes the need for those cursed script tags appended to the bottom of a page; something which I’ve never liked doing but became a bit of a necessary burdon! In short it allows you to execute arbitrary code on elements in the DOM using CSS selectors; so, for example, you can apply a onclick event only to elements which match <code>form fieldset div#items a.add</code><sup>1</sup>, very handy indeed!Outside of the web I got my hands on a copy of <a href="http://www.amazon.co.uk/exec/obidos/ASIN/B0007A5FZS/qid=1141728007/sr=8-1/ref=sr_8_xs_ap_i1_xgl/026-0724979-2170829">Star Wars: Empire at War</a>, which is easily the best Star Wars RTS game I’ve played (which isn’t hard). Very entertaining, if a little samey after a while. Definitely worth playing though.</p>
<p>Even further outside of the web — in the real world — Sara and I got tickets for the Foo Fighters, Queens of the Stone Age and Motörhead show in Hyde Park, London. That should be an entertaining night and it’s a very good excuse to get ourselfs down to London again, a city we really enjoyed last time.</p>
<p><sup>1</sup> Any anchors with a class of “add”, within a div with an ID of “items” that itself is within a fieldset in a form tag.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/just-so-you-know/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Venkman for Firefox 1.5.0.1 (Javascript Debugger)</title>
		<link>http://jagregory.com/writings/venkman-for-firefox-1501-javascript-debugger/</link>
		<comments>http://jagregory.com/writings/venkman-for-firefox-1501-javascript-debugger/#comments</comments>
		<pubDate>Tue, 07 Feb 2006 21:49:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Nuggets]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=26</guid>
		<description><![CDATA[Once again the guys at Mozilla have broken quite possibly the best plug-in for Firefox: the Venkman Javascript Debugger. This is becoming quite commonplace, with every update for firefox disabling the debugger until a fix is released.
Anyway, even though both the extensions manager and Venkmans official website both say there aren’t any updates available, Joe [...]]]></description>
			<content:encoded><![CDATA[<p>Once again the guys at Mozilla have broken quite possibly the best plug-in for Firefox: the <a href="http://www.mozilla.org/projects/venkman/">Venkman Javascript Debugger</a>. This is becoming quite commonplace, with every update for firefox disabling the debugger until a fix is released.</p>
<p>Anyway, even though both the extensions manager and Venkmans official website both say there aren’t any updates available, Joe Walker from <a href="http://getahead.ltd.uk/home">Getahead</a> has created and update that contains some bug fixes from Bugzilla, but most importantly Venkman now works with Firefox 1.5.0.1!</p>
<p>Source: <a href="http://getahead.ltd.uk/ajax/venkman">Venkman for Firefox 1.5</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/venkman-for-firefox-1501-javascript-debugger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript getNextElement and getPreviousElement &#8211; revised</title>
		<link>http://jagregory.com/writings/javascript-getnextelement-and-getpreviouselement-revised/</link>
		<comments>http://jagregory.com/writings/javascript-getnextelement-and-getpreviouselement-revised/#comments</comments>
		<pubDate>Thu, 12 Jan 2006 21:31:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=29</guid>
		<description><![CDATA[Please, stop reading a four year old post and just download jQuery.
Download: domnavigation-1.1.js
As I said I would be in my previous post, I’ve been working on improving getNextElement. I’ve improved the overall performance of the function and also created a few others with similar purpose; including the sister function getPrevious element. I outline all the [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Please, stop reading a four year old post and just <a href="http://jquery.com/">download jQuery</a>.</p></blockquote>
<p>Download: <a href="http://www.jagregory.com/downloads/domnavigation-1.1.js" title="DOM Navigation javascript file">domnavigation-1.1.js</a></p>
<p>As I said I would be in my previous post, I’ve been working on improving getNextElement. I’ve improved the overall performance of the function and also created a few others with similar purpose; including the sister function getPrevious element. I outline all the functions and their uses below; you can find the download link at the top and bottom of this post.</p>
<p>Any bugs anyone finds don’t fret to email me or post a comment here, i’ll work on fixing them asap.</p>
<h3>The Functions</h3>
<h4>getNextElement &amp; getPreviousElement</h4>
<p>Basically this function is a more advanced, flexible version of the “element.nextSibling” and “element.previousSibling” properties. Features include the ability to specify the next element you’e looking for explicitly, so you can return the next list-item in a list (even if it happens to be a child of the current element) or the next matching node type. Also recursion is toggleable.</p>
<h4>getFirstChild &amp; getLastChild</h4>
<p>A core part of getNextElement/getPreviousElement, this function recursively searches an elements child nodes for a matching element. Can be used stand alone to only return from within an elements child structure; recursively parses the childNode structures of all child elements.</p>
<h4>getNextParent &amp; getPreviousParent</h4>
<p>Gets the next node in the DOM tree after the current nodes parent; this walks the DOM tree backwards until it finds a parent of the current node with a nextSibling/previousSibling present.</p>
<h4>isType</h4>
<p>Detects whether a specified element is of correct type; compares against a numerical value (nodeType) or a string value (tagName).</p>
<h3>The Examples</h3>
<p>Note: All functions that require an element as a parameter can either take a direct reference or a string ID value.</p>
<h4>getNextElement &amp; getPreviousElement</h4>
<p>Both functions take exactly the same parameters and are used in the same way, so I’ll only provide examples for one.</p>
<p><span class="note"><br />
Note: These functions also walk up the DOM tree, so if you have two lists and call getNextElement on the last list-item in the first list it will return the first item of the second list.<br />
</span></p>
<p><code>getNextElement(id);</code></p>
<p>Returns the next element, regardless of type, after id.</p>
<p><code>getNextElement(id, 'li');</code></p>
<p>Returns the next list item, after id; could potentially be a child of id.</p>
<p><code>getNextElement(id, 1);</code></p>
<p>Returns the next element with a nodeType of 1.</p>
<p><code>getNextElement(id, 'li', false);</code></p>
<p>Returns the next list-item that resides on the same level, or higher, as id; no child nodes are searched.</p>
<h4>Practical example:</h4>
<pre><code>&lt;div id="popup"&gt;
    &lt;h1&gt;This is a popup&lt;/h1&gt;
    &lt;p id="paragraph"&gt;Below are some options:&lt;/p&gt;
    &lt;ul id="list"&gt;
        &lt;li&gt;Your first choice&lt;/li&gt;
        &lt;li id="secondItem"&gt;Your second choice&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
</code></pre>
<p><code>getNextElement('popup');</code></p>
<p>Would most likely return whitespace.</p>
<p><code>getNextElement('popup', 1);</code></p>
<p>Would return the H1 tag.</p>
<p><code>getNextElement('paragraph', 'li');</code></p>
<p>Would return the first list-item, “Your first choice”.</p>
<p><code>getPreviousElement('secondItem', 'ul');</code></p>
<p>Would return the UL element.</p>
<p><code>getPreviousElement('paragraph', 1);</code></p>
<p>Would return the paragraph element.</p>
<h4>getFirstChild &amp; getLastChild</h4>
<p>Once again, both these functions take the same parameters and behave the same way so I’ll only provide examples for one.</p>
<p><code>getFirstChild(id);</code></p>
<p>Returns the first child of id, regardless of type.</p>
<p><code>getFirstChild(id, 'li');</code></p>
<p>Returns the first child of id that is a list-item.</p>
<p><code>getFirstChild(id, 1);</code></p>
<p>Returns the first child of id that has a nodeType of 1.</p>
<h4>Practical example:</h4>
<pre><code>&lt;div id="popup"&gt;
    &lt;h1&gt;This is a popup&lt;/h1&gt;
    &lt;p&gt;Below are some options:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;Your first choice&lt;/li&gt;
        &lt;li&gt;Your second choice&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
</code></pre>
<p><code>getFirstChild('popup');</code></p>
<p>Would most likely return a Text node with some whitespace.</p>
<p><code>getFirstChild('popup', 1);</code></p>
<p>Would return the H1 element.</p>
<p><code>getFirstChild('popup', 'li');</code></p>
<p>Would return the first list-item, “Your first choice”.</p>
<p><code>getLastChild('popup');</code></p>
<p>Once again, probably whitespace.</p>
<p><code>getLastChild('popup', 1);</code></p>
<p>Would return the UL element.</p>
<p><code>getLastChild('popup', 'li');</code></p>
<p>Would return the last list-item, “Your second choice”.</p>
<h4>getNextParent &amp; getPreviousParent</h4>
<p>Again, both these functions take the same parameters, so I’ll only show one.</p>
<p><code>getNextParent(id);</code></p>
<p>Returns the node next to the parent of id.</p>
<h4>Practical example:</h4>
<pre><code>&lt;ul&gt;
    &lt;li id="first"&gt;Item One&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li id="second"&gt;Item Two&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<p><code>getNextParent('first');</code></p>
<p>Would return the second UL element.</p>
<p><code>getPreviousParent('second');</code></p>
<p>Would return the first UL element.</p>
<h4>isType</h4>
<p><code>isType(id, 'li');</code></p>
<p>Returns true if id has a tagName of ‘li’.</p>
<p><code>isType(id, 3);</code></p>
<p>Returns true if id has a nodeType of 3.</p>
<p>Download: <a href="http://www.jagregory.com/downloads/domnavigation-1.1.js" title="DOM Navigation javascript file">domnavigation-1.1.js</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/javascript-getnextelement-and-getpreviouselement-revised/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
