<?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>Slightlymore &#187; HTML / XHTML</title>
	<atom:link href="http://slightlymore.co.uk/tag/xhtml/feed/" rel="self" type="application/rss+xml" />
	<link>http://slightlymore.co.uk</link>
	<description>The online residence of Clinton the intertube sorcerer</description>
	<lastBuildDate>Sat, 24 Jul 2010 17:11:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-RC2-15161</generator>
		<item>
		<title>CSS Naked Day</title>
		<link>http://slightlymore.co.uk/css-naked-day/</link>
		<comments>http://slightlymore.co.uk/css-naked-day/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 16:12:10 +0000</pubDate>
		<dc:creator>Clinton Montague</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[External links]]></category>
		<category><![CDATA[HTML / XHTML]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[semantics]]></category>

		<guid isPermaLink="false">http://slightlymore.co.uk/?p=522</guid>
		<description><![CDATA[What a bloomin' marvellous idea. On CSS naked day (9th April this year), you might notice a lot of websites loose thier styling. It's the brain child of Dustin Diaz, and it's aim is to help us all to write more semantic (X)HTML. By considering the design with no CSS, it forces us to write better and better structured markup. Will you be joining in this year?]]></description>
			<content:encoded><![CDATA[<p>What a bloomin&#8217; marvellous idea. On CSS naked day (9th April this year), you might notice a lot of websites loose thier styling. It&#8217;s the brain child of Dustin Diaz, and it&#8217;s aim is to help us all to write more semantic (X)HTML. By considering the design with no CSS, it forces us to write better and better structured markup. Will you be joining in this year?</p>
]]></content:encoded>
			<wfw:commentRss>http://slightlymore.co.uk/css-naked-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash and the z-index problem, solved</title>
		<link>http://slightlymore.co.uk/flash-and-the-z-index-problem-solved/</link>
		<comments>http://slightlymore.co.uk/flash-and-the-z-index-problem-solved/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 21:14:08 +0000</pubDate>
		<dc:creator>Clinton Montague</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[HTML / XHTML]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[swfobject]]></category>

		<guid isPermaLink="false">http://slightlymore.co.uk/?p=474</guid>
		<description><![CDATA[I was always told that Flash would render on top of everything else. That's it. There's no way around it. It meant that whenever I had a design using, say, suckerfish menus, I'd have to be careful to ensure that there was enough space below the menu to accommodate for Flash's nasty little habit. It turns out that the notorious <em>they</em> were wrong. Again. It's amazing just how many times 'they' don't get it quote right, and that the majority of people still believe them.]]></description>
			<content:encoded><![CDATA[<p>I was always told that Flash would render on top of everything else. That&#8217;s it. There&#8217;s no way around it. It meant that whenever I had a design using, say, suckerfish menus, I&#8217;d have to be careful to ensure that there was enough space below the menu to accommodate for Flash&#8217;s nasty little habit. It turns out that the notorious <em>they</em> were wrong. Again. It&#8217;s amazing just how many times &#8216;they&#8217; don&#8217;t get it quite right, and that the majority of people still believe <em>them</em>.</p>
<p>Just a quick side note &#8211; I&#8217;ll be using the excellent sfwobject.js in this tutorial rather than embedding the flash directly in the HTML. </p>
<h4>The problem</h4>
<p>By default, the Flash plugin will do a weird thing. Instead of being displayed <em>inside</em> the browser, it actually makes a new window <em>on top</em> of the browser which synchronises it&#8217;s position with where you&#8217;d expect the flash should be. (Well, actually it&#8217;s a bit more complicated than that, but I won&#8217;t bore you with the nitty-gritty). This means that rather annoyingly, unless you know the fix, flash will always be on top of your other content. Even if you set the z index of the flash to 0 and of your content to a bazillion. <a href="/examples/flash-z-index/broken.html">Take a look at the example</a>.</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
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:<span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=iso-8859-1&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Flash and the z-index problem. Solved<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;./swfobject.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
	#test
	{
		position: absolute;
		top: 300px;
		left: 300px;
		width: 400px;
		background-color: #ccc;
		padding: 10px;
		color: #333;
		font-family: georgia;
		font-size: 2em;
	}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">bgcolor</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#ffffff&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flash&quot;</span>&gt;</span>This is some flash content - you'll need the flash plugin to see it<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;test&quot;</span>&gt;</span>HTML on top of the flash. This page was created for the tutorial <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://slightlymore.co.uk/wisdom/flash-and-the-z-index-problem-solved&quot;</span>&gt;</span>Flash and the z-index problem. Solved<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span> on <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://slightlymore.co.uk&quot;</span>&gt;</span>slightlymore<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
	var so = new SWFObject(&quot;wmode.swf&quot;, &quot;wmode.swf&quot;, &quot;550&quot;, &quot;400&quot;, &quot;8&quot;,&quot;#ffffff&quot;);
	so.write(&quot;flash&quot;);
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></td></tr></table></div>

<h4>The solution</h4>
<p>The solution is simple. With an extra parameter, you can force the flash to render in a different <em>window mode</em>. The window mode that&#8217;s of interest to solve this problem is <del><em>transparent</em></del> <em>opaque</em> (see the <a href="#comment-479">comment by philip</a>). It means (again, in simple terms) that the flash is this time rendered not only inside the browser window, but as if it were a normal image in the document flow instead of in a separate window on top of the browser window. With swfobject, it&#8217;s almost too easy.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">so.<span style="color: #660066;">addParam</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;wmode&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;opaque&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Or if you want to continue being old-school, you could use the following line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;wmode&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;opaque&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></td></tr></table></div>

<p>Adding everything together will leave you something similar to the code below. Again, I have prepared an <a href="http://slightlymore.co.uk/examples/flash-z-index/">example page</a> for you to look at.</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
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:<span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=iso-8859-1&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Flash and the z-index problem. Solved<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;./swfobject.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
	#test
	{
		position: absolute;
		top: 300px;
		left: 300px;
		width: 400px;
		background-color: #ccc;
		padding: 10px;
		color: #333;
		font-family: georgia;
		font-size: 2em;
	}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">bgcolor</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#ffffff&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flash&quot;</span>&gt;</span>This is some flash content - you'll need the flash plugin to see it<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;test&quot;</span>&gt;</span>HTML on top of the flash. This page was created for the tutorial <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://slightlymore.co.uk/wisdom/flash-and-the-z-index-problem-solved&quot;</span>&gt;</span>Flash and the z-index problem. Solved<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span> on <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://slightlymore.co.uk&quot;</span>&gt;</span>slightlymore<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
	var so = new SWFObject(&quot;wmode.swf&quot;, &quot;wmode.swf&quot;, &quot;550&quot;, &quot;400&quot;, &quot;8&quot;,&quot;#ffffff&quot;);
	// this next line is the important one
	so.addParam(&quot;wmode&quot;, &quot;opaque&quot;);
	so.write(&quot;flash&quot;);
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></td></tr></table></div>

<h4>Grab the files</h4>
<p>I have zipped up all the files used in this tutorial for <a href="/examples/flash-z-index/flash-z-index.zip">you to download</a>.</p>
<h4>Application</h4>
<p>I first saw this being implemented on <a href="http://www.mitchelsliwa.com/">www.mitchelsliwa.com</a> and assumed that the man must have been dabbling with black magic. I&#8217;m currently working on a huge project which uses this code so that we can get a lightbox over a flash site. Where else have you seen this being used?</p>
]]></content:encoded>
			<wfw:commentRss>http://slightlymore.co.uk/flash-and-the-z-index-problem-solved/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Progressive enhancement with CSS3</title>
		<link>http://slightlymore.co.uk/progressive-enhancement-with-css3/</link>
		<comments>http://slightlymore.co.uk/progressive-enhancement-with-css3/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 10:10:38 +0000</pubDate>
		<dc:creator>Clinton Montague</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[External links]]></category>
		<category><![CDATA[2.0]]></category>
		<category><![CDATA[buttons]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTML / XHTML]]></category>

		<guid isPermaLink="false">http://slightlymore.co.uk/?p=266</guid>
		<description><![CDATA[This rather good tutorial explains how to create brilliant 2.0-esque buttons using only text and CSS &#8211; NO IMAGES. It shows you how to take advantage of the slow emergence of browsers which understand CSS3 properties such as border radius and the like. Even if you&#8217;re not going to use this method, it&#8217;s a useful [...]]]></description>
			<content:encoded><![CDATA[<p>This rather good tutorial explains how to create brilliant 2.0-esque buttons using only text and CSS &#8211; NO IMAGES. It shows you how to take advantage of the slow emergence of browsers which understand CSS3 properties such as border radius and the like. Even if you&#8217;re not going to use this method, it&#8217;s a useful tool for the future</p>
]]></content:encoded>
			<wfw:commentRss>http://slightlymore.co.uk/progressive-enhancement-with-css3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Better, faster and more robust rollovers with CSS sprites</title>
		<link>http://slightlymore.co.uk/better-faster-rollovers-with-css-sprites/</link>
		<comments>http://slightlymore.co.uk/better-faster-rollovers-with-css-sprites/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 19:35:57 +0000</pubDate>
		<dc:creator>Clinton Montague</dc:creator>
				<category><![CDATA[Accessibility / Usability]]></category>
		<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Google / SEO]]></category>
		<category><![CDATA[HTML / XHTML]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[sprites]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://slightlymore.co.uk/?p=209</guid>
		<description><![CDATA[Learn how to create faster, standards compliant, javascript-less and accessible buttons using only CSS. Even the most hard-core of javascript programmers will admit that actually, they might have done this by mistake. And I promise to show you how, and if you're lucky explain why.

I'll even tell you what a sprite is.]]></description>
			<content:encoded><![CDATA[<p>Do you remember the olden days? You know, the ones where we had 56k modems, a mouse with only two buttons, and the only way to have a rollover state were those horrid MM_SwapImage javascript functions (or whatever they were called). I do. And I remember that it was actually only a small number of years ago. A large proportion of people are still using javascript for this task, maybe they&#8217;re stuck in their old ways. I want to show you a much sleeker, and if I&#8217;m honest, a much sexier way to do the same thing. If you can&#8217;t wait, I have put together <a href="/examples/css-sprites/">an example</a>.</p>
<h4>So what&#8217;s a sprite</h4>
<p>A sprite is essentially a picture. They can be 2d or 3d and are normally animated or move in some way. Strange to use a different word, but you know what us geeks are like. We like to make things sounds more complicated than they actually are, all the time. How many acronyms go into the technology of this page, for instance. WordPress is built in <em  class="highlight">PHP</em> using the <em  class="highlight">MySQL</em> database. It was uploaded with <em class="highlight">FTP</em>, to a <em class="highlight">LAMP</em> server which I manage using <em class="highlight">SSH</em>. The pages are serverd over <em class="highlight">HTTP</em> and are <em class="highlight">XHTML</em> which is styled using <em class="highlight">CSS</em> which is also used to position the <em class="highlight">PNG</em> image in the header. Ok, ok, I&#8217;ll stop, you get the picture. So a sprite is an image. Think of a picture of Sprite<sup>tm</sup> if that will help. (<em class="highlight">lol</em>?)</p>
<h4>What&#8217;s wrong with Javascript?</h4>
<p>Nothing! Please, be my guest, continue using javascript until your heart is content. But it <em>will</em> become content. It&#8217;s a lot of work having to put your images in an array, get the hover states working independently and hitting your head against your desk. Then ofcourse, there&#8217;s always that annoying paranoid troglodyte of a client who has their javascript disabled so can&#8217;t see your beautiful handiwork.</p>
<h4>Why is your CSS method so great then, huh??!</h4>
<p>Sure, my client could have CSS disabled just like yours had javascript disabled. But then, my CSS disabling client is <em>used</em> to only seeing the web in 16pt times new roman with blue links. Using this CSS instead of swapping images will, believe it or not, improve the semantic meaning of your code. Which means google and friends will understand your site better too! Sounds too good to be true, doesn&#8217;t it! Shall we press on?</p>
<h4>The theory</h4>
<p>The idea here is to create a fully standards compliant, fully accessible and gracefully degradable graphical button using only HTML and CSS.</p>
<p>Let&#8217;s start with the the HTML for a graphic and turn it into a link, like a normal image link.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://slightlymore.co.uk/&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button.png&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;My button&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>

<p>Problem number 1: It&#8217;s not accessible. Well, sure, it&#8217;s got an alt tag, but let&#8217;s be pedantic here. Solution: Let&#8217;s remove the image and replace it with the text. After that, set the <em>a</em> tag to be displayed as a block (which allows you to specify the height and width), give it an id (so we can pick it out), give it a class of button (there will be other buttons like it so may as well use this to style it up) then use the picture as a background image.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;">	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://slightlymore.co.uk/&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sexy-button&quot;</span>&gt;</span>Sexy button<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.button</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">184px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">35px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#sexy-button</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">sexy-button.png</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Great! Oh, except for the fact that the text is over the button. So let&#8217;s surround the text with a <code>span</code>, then tell the span to go away with CSS.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.button</span> span
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-9999px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h4>Wasn&#8217;t it supposed to move?</h4>
<p>Welcome to the magic of CSS sprites. Remember how those rollover buttons were made of two graphics, one for the on state and one for the off state? Well use some photoshop glue to place them on top of each other (the off state at the top). I know, I know, it&#8217;s bigger than the size of the background, but that&#8217;s the awesome thing! Now, using only css, we can <em>move the position of the background</em> when the user rolls over the anchor tag using the :hover psuedo class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.button</span><span style="color: #3333ff;"><span style="color: #00AA00;">:</span>hover
</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">-35px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* moving up by the height of the button */</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><em>AND</em> because it&#8217;s just a single image, the user won&#8217;t see the short delay and flicker when they roll over your button because the browser doesn&#8217;t have to load a new image!</p>
<p>The applications of this are immense. Just imagine having the on, off and active states all in one image and all controlled with your new friend, css. If you have gone on to use this, post a link to your new page!</p>
<p>And for graceful degradation:</p>
<ul>
<li>Javascript disabled: Great, we didn&#8217;t use it anyway</li>
<li>CSS disabled: Great, you&#8217;ll see the text link</li>
<li>Google: Great, keep your text contextual (i.e. describe where the user will go instead of &#8220;click here&#8221;)</li>
<li>Screen readers: Great, there were no images involved so the program can read the text</li>
</ul>
<p>You can <a href="/examples/css-sprites/css-sprites.zip">download the files</a> in the <a href="/examples/css-sprites/">example page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://slightlymore.co.uk/better-faster-rollovers-with-css-sprites/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Seperating HTML, CSS and Javascript &#8211; unobtrusive javascript</title>
		<link>http://slightlymore.co.uk/seperating-html-and-javascript-unobstrusive-javascript/</link>
		<comments>http://slightlymore.co.uk/seperating-html-and-javascript-unobstrusive-javascript/#comments</comments>
		<pubDate>Sun, 16 Nov 2008 19:43:53 +0000</pubDate>
		<dc:creator>Clinton Montague</dc:creator>
				<category><![CDATA[Accessibility / Usability]]></category>
		<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML / XHTML]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[unobstrusive]]></category>

		<guid isPermaLink="false">http://slightlymore/?p=15</guid>
		<description><![CDATA[So with all of this web 2.0 hype you want to add in loads of cool CSS behaviours, javascript wotsits and AJAX wangdoodles to your website. But then you're told that you have to keep the (X)HTML clean. 'What the?!!' you may ask. Read on to find out how the voodoo is done.]]></description>
			<content:encoded><![CDATA[<p><em class="highlight">Please note!! I have shamelessly stolen this article from my old and (even if I do say so myself) rather boring blog. So please ignore the terrible writing style! I&#8217;ll rewrite this article in the future, but until then, I present you with version 1.0</em></p>
<p>In todays world of &#8216;standards this&#8217;, &#8216;accessibility that&#8217; and &#8216;ajax the other&#8217;, it can be a little overwhelming and confusing. How, for example, are you <em>supposed</em> to be able to add in all of this lovely AJAX scripting without being allowed to add javascript anywhere on your page to keep it accessible?!</p>
<p>Luckily, there is a way. Read on if you would like to know how this black magic is done&#8230;</p>
<p>First things first, let&#8217;s decide exactly what files we will need for the tutorial. There are three parts to a web page. The structure (xhtml), the styling (css), and thirdly, and behaviour (javascript). These should be three <strong>distinct</strong> parts. An HTML tag such as</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text-decoration: underline;&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;doSomething()&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;somewhere.html&quot;</span>&gt;</span>Click me<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>

<p>is very naughty indeed.</p>
<p>Your (x)html should look as if it has no styling and no javascript applied to it (other than the &#8216;class&#8217; and &#8216;id&#8217; elements applied to a tag). How, then, are you supposed to get anything to do anything? The good news is that you&#8217;re &#8216;allowed&#8217; to include css and javascript from other files, keeping your html looking tidy.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- this is ok --&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;the_script.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--mce:0--&gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--mce:1--&gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>So, how to get event handers (onclick) etc from the external file into the tags of you html. Well there is a brilliant technology called the DOM (document object model). Using this, it is possible to find and edit the tags and their attributes. There are two great functions to do this with, getElementById and getElementsByTagName. Using these, the possibilities are endless! What you must do it load the html, then get the browser to do a little work and update the markup to do the dynamic things which you want it to do.</p>
<p>One function that they seemed to overlook was getElementsByClassName, which is quite strange considering the other two functions. Luckily, there are other developers in the world who have already noticed this, so there are many already written. I use Robert Nyman&#8217;s <a href="http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/">getElementsByClassName</a> function.</p>
<p>So, to modify the tags, you first search for them using one of these javascript functions. Then, when you have found the tag (or group of tags) which you want to edit, you edit them with javascript. It&#8217;s really not as scary as it sounds if you write functions to do this for you, and luckily, <strong>I</strong> have written a function to do this for you. It has quite a few options, including changing the class name, adding html to the element (adding more functionality might need more explaination), and ofcourse adding the onclick attribute.</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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// usage: injectBehaviour ('myClass', {'onclick':functionName})</span>
<span style="color: #006600; font-style: italic;">// options in the array are:</span>
<span style="color: #006600; font-style: italic;">//		onclick</span>
<span style="color: #006600; font-style: italic;">//		addclassname</span>
<span style="color: #006600; font-style: italic;">//		replaceclassname</span>
<span style="color: #006600; font-style: italic;">//		addtitle</span>
<span style="color: #006600; font-style: italic;">//		replacetitle</span>
<span style="color: #006600; font-style: italic;">//		htmlbefore</span>
<span style="color: #006600; font-style: italic;">//		htmlafter</span>
<span style="color: #006600; font-style: italic;">//		htmlreplace</span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">//		if both add* and replace* are specified, 'replace' takes precidence</span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">//		if htmlbefore / htmlafter are specified, they will be added before</span>
<span style="color: #006600; font-style: italic;">//		or after the original innerHTML or the htmlreplace specified</span>
<span style="color: #003366; font-weight: bold;">function</span> injectBehaviour<span style="color: #009900;">&#40;</span>className<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> elements<span style="color: #339933;">=</span>getElementsByClassName <span style="color: #009900;">&#40;</span>document<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> className<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>elements.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #009900;">&#93;</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: #000066; font-weight: bold;">typeof</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span><span style="color: #339933;">=</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span>
			<span style="color: #009900;">&#123;</span>
				elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<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;">try</span><span style="color: #009900;">&#123;</span>
											<span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
										<span style="color: #009900;">&#125;</span>
										<span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
										<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;addclassname&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span><span style="color: #339933;">+=</span><span style="color: #009900;">&#40;</span>elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;addclassname&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;addclassname&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;replaceclassname&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;replaceclassname&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;addtitle&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">title</span><span style="color: #339933;">+=</span>elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;addtitle&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;addtitle&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;replacetitle&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;replacetitle&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;htmlreplace&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;htmlreplace&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;htmlbefore&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;htmlbefore&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;htmlafter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> elements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;htmlafter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice how in the onclick part of the function, it is a slightly more complicated than the other parts. This is so that you can either pass a &#8216;hard coded&#8217; function (for example &#8216;alert(&#8220;Hello, world&#8221;)&#8217;) or pass a &#8216;dynamic&#8217; function whose parameters might change depending on the element (if for example building a dynamic list). If the latter is true, I have surrounded the execution block with a try/catch statement just incase there are any syntax errors in the text you supply. To pass the element to this function, simply use the word &#8216;this&#8217; and it will work. I realise that this is quite difficult to explain/understand in writing, so here is a coded example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;dave&quot;</span> <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;makeMeSayHello&quot;</span> href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Click me<span style="color: #339933;">!&lt;/</span>a<span style="color: #339933;">&gt;</span>
&nbsp;
injectBehaviour <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;makeMeSayHello&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'onclick'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'alert(&quot;The element with id &quot;+this.id+&quot; is saying hello!&quot;)'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I know, this is a bit of a pointless script, but you get the idea.</p>
<p>Now, here&#8217;s the problem: You want to call this function LOADS of times, but you only have one onLoad handler for the body tag. What do you do?!??! Here&#8217;s your answer (aren&#8217;t you lucky!)</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// allows for multiple onload functions to be added to the</span>
<span style="color: #006600; font-style: italic;">// document. instead of using  use</span>
<span style="color: #006600; font-style: italic;">// addOnload (function).</span>
<span style="color: #003366; font-weight: bold;">function</span> addOnload<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> oldOnload <span style="color: #339933;">=</span> window.<span style="color: #000066;">onload</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> oldOnload <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		window.<span style="color: #000066;">onload</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;">if</span> <span style="color: #009900;">&#40;</span>oldOnload<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				oldOnload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			func<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: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So now all you do at the bottom of your page is this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--mce:2--&gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>So to sum up, never again will you be putting onAnything handlers ANYWHERE in your html, will you!</p>
<p>P.S. That doesn&#8217;t mean you can use this either!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript:myFunction()&quot;</span>&gt;</span>Naughty<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://slightlymore.co.uk/seperating-html-and-javascript-unobstrusive-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
