<?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>Jeremy Smyth's Blog &#187; javascript</title>
	<atom:link href="http://jeremysmyth.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeremysmyth.com</link>
	<description></description>
	<lastBuildDate>Sun, 04 Jul 2010 07:10:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Accessibility: HTML Keyboard Shortcuts with &#8220;accesskey&#8221;</title>
		<link>http://jeremysmyth.com/2010/03/29/accessibility-keyboard-shortcuts-on-the-web-with-accesskey/</link>
		<comments>http://jeremysmyth.com/2010/03/29/accessibility-keyboard-shortcuts-on-the-web-with-accesskey/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 09:59:22 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=237</guid>
		<description><![CDATA[
function showKeys() {
	if (document.styleSheets) {
		var s = document.styleSheets[0];
		var l = s.cssRules.length;
		s.insertRule("[accesskey]:after {font-weight: 700; border-bottom: 1px blue dotted; content: '[' attr(accesskey) ']';}", l);
	} 
}

Accessibility is a hot topic on the web, and there are many emerging standards to help move in an accessible direction. We have CSS media types, alternative web pages with simpler navigation and [...]]]></description>
			<content:encoded><![CDATA[<p><script language="Javascript1.2">
function showKeys() {
	if (document.styleSheets) {
		var s = document.styleSheets[0];
		var l = s.cssRules.length;
		s.insertRule("[accesskey]:after {font-weight: 700; border-bottom: 1px blue dotted; content: '[' attr(accesskey) ']';}", l);
	} 
}
</script></p>
<p>Accessibility is a hot topic on the web, and there are many emerging standards to help move in an accessible direction. We have CSS media types, alternative web pages with simpler navigation and high-contrast styling, <tt>alt</tt> and <tt>title</tt> tags, and the general move away from mixing style with substance.</p>
<p>Along with all of this, we have the relatively old standard of using the <tt>accesskey</tt> attribute on invokeable elements such as hyperlinks and form inputs. This allows us to attach a keyboard shortcut to elements in our webpage.</p>
<p>Unfortunately, this isn&#8217;t a widely used, or easily implemented standard. Although the accesskey attribute is widely supported, only one commonly-used browser (Opera) at the time of writing provides an easy way for users to see what accesskeys are enabled on a given site, and there is no widely-accepted standard for choosing which accesskeys perform which function.</p>
<p>However, with a little jiggery-pokery we can implement a simple way to show accesskeys on demand:</p>
<div style="width: 90%; border: 1px dashed gray; margin-left: auto; margin-right: auto; padding: 8px;">
<input type="button" value="Show access keys" onclick="showKeys()" style="float: right;" />
<p><a href="http://jeremysmyth.com/2010/03/29/accessibility-keyboard-shortcuts-on-the-web-with-accesskey/"     title="Link to this post"  accesskey="9"><br />
This link</a> will bring you to this post&#8217;s permalink, and can be actuated with the accesskey &#8220;9&#8243;; in Firefox, you hold alt-shift and press 9. </p>
</div>
<p />
<p>The code for the above is pretty simple. First, the button:</p>
<pre>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/input.html"><span class="kw2">input</span></a> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;button&quot;</span> <span class="kw3">value</span><span class="sy0">=</span><span class="st0">&quot;Show access keys&quot;</span> </span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; <span class="kw3">onclick</span><span class="sy0">=</span><span class="st0">&quot;showKeys()&quot;</span> <span class="kw3">style</span><span class="sy0">=</span><span class="st0">&quot;float: right;&quot;</span> <span class="sy0">/</span>&gt;</span></div>
</div>
</pre>
<p>The hyperlink itself (abbreviated):</p>
<pre>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/a.html"><span class="kw2">a</span></a> <span class="kw3">href</span><span class="sy0">=</span><span class="st0">&quot;http://jeremysmyth.com/....&quot;</span> </span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; <span class="kw3">title</span><span class="sy0">=</span><span class="st0">&quot;Link to this post&quot;</span> </span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; <span class="kw3">accesskey</span><span class="sy0">=</span><span class="st0">&quot;9&quot;</span>&gt;</span>This link<span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/a.html"><span class="kw2">a</span></a>&gt;</span> 
will bring you...</div>
</div>
</pre>
<p>Finally, the javascript:</p>
<pre>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="kw2">function</span> showKeys<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>document.<span class="me1">styleSheets</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> sheet <span class="sy0">=</span> document.<span class="me1">styleSheets</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> len <span class="sy0">=</span> sheet.<span class="me1">cssRules</span>.<span class="me1">length</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// reformatted to fit</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sheet.<span class="me1">insertRule</span><span class="br0">&#40;</span><span class="st0">&quot;[accesskey]:after {&quot;</span> <span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;font-weight: 700; &quot;</span> <span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;border-bottom: 1px blue dotted; &quot;</span> <span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;content: '[' attr(accesskey) ']';}&quot;</span> <span class="sy0">,</span> len<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Clicking on the button calls the &#8220;showKeys()&#8221; function in the Javascript script block, which adds a style to the current stylesheet. The style automatically styles elements with the &#8220;accesskey&#8221; attribute, adding the value of that attribute after the element itself.
</p>
<p>Put simply, it adds a styled [9] after the hyperlink, because (1) it has the &#8220;accesskey&#8221; attribute, and secondly, the &#8220;9&#8243; is the value of that attribute, as calculated by the <tt>attr()</tt> function.</p>
<p><em style="font-style: italic; font-size: 90%;">Note: The above Javascript won&#8217;t currently work in Internet Explorer; for that, you&#8217;d need Microsoft&#8217;s <a href="http://msdn.microsoft.com/en-us/library/aa358796%28VS.85%29.aspx" title="Microsoft StyleSheet reference">addRule</a> function rather than the standards-compliant <a href="http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet" title="W3 StyleSheet standards site">insertRule</a> I&#8217;ve used.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2010/03/29/accessibility-keyboard-shortcuts-on-the-web-with-accesskey/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8216;Hello World&#8217; using AJAX</title>
		<link>http://jeremysmyth.com/2009/08/20/hello-world-using-ajax/</link>
		<comments>http://jeremysmyth.com/2009/08/20/hello-world-using-ajax/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 14:16:54 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=53</guid>
		<description><![CDATA[&#8220;Asynchronous Javascript And XML&#8221; is a mouthful for a web technology that&#8217;s in wide use these days. Simply put, it lets a web page download more content without reloading the page, and relies on a magic Javascript object called an XmlHttpRequest to do the work of requesting more data in the background, so it can [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Asynchronous Javascript And XML&#8221; is a mouthful for a web technology that&#8217;s in wide use these days. Simply put, it lets a web page download more content without reloading the page, and relies on a magic Javascript object called an XmlHttpRequest to do the work of requesting more data in the background, so it can fill in things on the page.</p>
<p>So, fiddling with AJAX, let&#8217;s do a little Hello Worlding:</p>
<p><script type='text/javascript'>
<!--
var xhr;
if (window.XMLHttpRequest){
 xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject){ // in case it's IE
 xhr = new ActiveXObject("Microsoft.XMLHTTP");
 }
// set up the callback
xhr.onreadystatechange = function(){
 myDiv = document.getElementById("ajaxtest");
 if(xhr.readyState  == 4) {
 if(xhr.status  == 200){
 myDiv.style.background = "green";
 myDiv.innerHTML =  xhr.responseText;
 } else {
 myDiv.innerHTML = "Error: " + xhr.status;
 }
 }  
}
function startRequest(){
 xhr.open('GET', '/ajaxtest.php', true);
 xhr.send(null);
}
-->
</script></p>
<p>Here&#8217;s a button, that when clicked, calls the &#8220;startRequest()&#8221; function in some javascript behind the entry:</p>
<pre>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/input.html"><span class="kw2">input</span></a> <span class="kw3">onclick</span><span class="sy0">=</span><span class="st0">&quot;startRequest()&quot;</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;button&quot;</span> <span class="kw3">value</span><span class="sy0">=</span><span class="st0">&quot;Hello World!&quot;</span> <span class="sy0">/</span>&gt;</span></div>
</div>
</pre>
<input onclick="startRequest()" type="button" value="Hello World!" />
<p />
<p>And here&#8217;s a div:</p>
<pre>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/div.html"><span class="kw2">div</span></a> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;ajaxtest&quot;</span>&gt;</span>
<span class="sc2">&lt;<a href="http://december.com/html/4/element/em.html"><span class="kw2">em</span></a>&gt;</span>(although you can't see it as a separate div, 
this text will be replaced)<span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/em.html"><span class="kw2">em</span></a>&gt;&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/div.html"><span class="kw2">div</span></a>&gt;</span></div>
</div>
</pre>
<div id="ajaxtest"><em>(although you can&#8217;t see it as a separate div, this text will be replaced)</em></div>
<p />
<p>Go on, watch the div text above, and click the button.</p>
<p>Here&#8217;s the javascript:</p>
<pre>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="kw2">var</span> xhr<span class="sy0">;</span>
<span class="kw1">if</span> <span class="br0">&#40;</span>window.<span class="me1">XMLHttpRequest</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; xhr <span class="sy0">=</span> <span class="kw2">new</span> XMLHttpRequest<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>window.<span class="me1">ActiveXObject</span><span class="br0">&#41;</span><span class="br0">&#123;</span> <span class="co1">// in case it's IE</span>
&nbsp; xhr <span class="sy0">=</span> <span class="kw2">new</span> ActiveXObject<span class="br0">&#40;</span><span class="st0">&quot;Microsoft.XMLHTTP&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;<span class="br0">&#125;</span>
<span class="co1">// set up the callback</span>
xhr.<span class="me1">onreadystatechange</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; myDiv <span class="sy0">=</span> document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&quot;ajaxtest&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>xhr.<span class="me1">readyState</span> &nbsp;<span class="sy0">==</span> 4<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>xhr.<span class="kw3">status</span> &nbsp;<span class="sy0">==</span> <span class="nu0">200</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp;myDiv.<span class="me1">style</span>.<span class="me1">background</span> <span class="sy0">=</span> <span class="st0">&quot;green&quot;</span><span class="sy0">;</span>
&nbsp; &nbsp;myDiv.<span class="me1">innerHTML</span> <span class="sy0">=</span> &nbsp;xhr.<span class="me1">responseText</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
&nbsp; &nbsp;myDiv.<span class="me1">innerHTML</span> <span class="sy0">=</span> <span class="st0">&quot;Error: &quot;</span> <span class="sy0">+</span> xhr.<span class="kw3">status</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="br0">&#125;</span> &nbsp;
<span class="br0">&#125;</span>
<span class="kw2">function</span> startRequest<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; xhr.<span class="kw3">open</span><span class="br0">&#40;</span><span class="st0">'GET'</span><span class="sy0">,</span> <span class="st0">'/ajaxtest.php'</span><span class="sy0">,</span> <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; xhr.<span class="me1">send</span><span class="br0">&#40;</span><span class="kw2">null</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p />
<p />Firstly, the code sets up a variable <tt>xhr</tt>, which contains the magical object. This is the <tt>XmlHttpRequest</tt> object, and is responsible for communicating from the live Javascript to the back end server.</p>
<p>The request object is first set up at the script&#8217;s execution, but the only thing we do with it is tell it what to do when its &#8220;ready state&#8221; changes; that is, when the request shifts from not initialised, to set up, to having been sent, to being in process, to completion. </p>
<p>These states are numbered 0-4 respectively. Hold that thought; we&#8217;ll use it later. Suffice to say that, on the script&#8217;s execution, we now have an otherwise anonymous function that will be called when the ready state changes.</p>
<p>Now, in order for something interesting to happen, we click the button. That kicks off <tt>startRequest()</tt>, which in turn sets up a asynchronous request to <tt>ajaxtest.php</tt>, and submits it with no parameters (that&#8217;s the <tt>null</tt>). Asynchronous means we don&#8217;t sit around waiting for a response, and that&#8217;s why we need the readystatechange callback function.</p>
<p>So, when something interesting happens to the xhr object, the ready state changes (e.g. when we get a response back after sending our request). The browser then executes the anonymous function: first, the function checks that the request is complete &#8211; remember Ready State 4 above? don&#8217;t forget the ready state changes several times per request. We only want to read the response text once, at completion. Once the function has checked the request has been successfully completed,  it then uses the xhr&#8217;s response-text to fill in the div called &#8220;ajaxtest&#8221;. </p>
<p>Et voila, we have &#8220;Hello World&#8221; in AJAX.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2009/08/20/hello-world-using-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
