<?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>Chris Tate-Davies &#187; Javascript</title>
	<atom:link href="http://www.christatedavies.co.uk/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christatedavies.co.uk</link>
	<description>An archive of helpful tit bits of information for development, and probably some stuff that is incomplete, wrong or boring...</description>
	<lastBuildDate>Tue, 31 Jan 2012 11:18:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Javascript Hoisting</title>
		<link>http://www.christatedavies.co.uk/2011/11/18/javascript-hoisting/</link>
		<comments>http://www.christatedavies.co.uk/2011/11/18/javascript-hoisting/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 12:08:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[global]]></category>
		<category><![CDATA[hoisting]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[var]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=279</guid>
		<description><![CDATA[When using JavaScript, you declare a variable using the var keyword. var myvariable = "a value"; These variables are globally accessible unless they are defined within a function. Javascript performs an operation known as &#8220;hoisting&#8221; when it compiles which draws all the declarations to the top of the page. Here is a piece of code: [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/11/18/javascript-hoisting/' addthis:title='Javascript Hoisting ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>When using JavaScript, you declare a variable using the var keyword.</p>
<pre>var myvariable = "a value";</pre>
<p>These variables are globally accessible unless they are defined within a function. Javascript performs an operation known as &#8220;hoisting&#8221; when it compiles which draws all the declarations to the top of the page.</p>
<p>Here is a piece of code:</p>
<pre>var test = "my test";
alert(test);</pre>
<p>When run, the above will display &#8220;my test&#8221; in an alert, as you would expect.</p>
<p>Consider this:</p>
<pre>var test = "my test";
alert(test);

function testMe() {
    alert(test);
    var test = "another test";
    alert(test);
}

testMe();</pre>
<p>The above will display &#8220;my test&#8221; and then an &#8220;undefined&#8221; error and then &#8220;another test&#8221; in an alert. This is because we have declared a function variable within the function testMe function and this overrides the global state.</p>
<p>If we refactor the code as so:</p>
<pre>var test = "my test";
alert(test);

function testMe() {
    alert(test);
    test = "another test";
    alert(test);
}

testMe();</pre>
<p>Because we have not used the var keyword, the variable exists throughout, and the alerts you get will be &#8220;my test&#8221; and then &#8220;my test&#8221; and finally, &#8220;another test&#8221;</p>
<p>Its very important to remember this, as when you have some large functions, you could be getting obscure results you could be looking for a long time to find the issue.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/11/18/javascript-hoisting/' addthis:title='Javascript Hoisting ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/11/18/javascript-hoisting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Viewing events attached to elements via JavaScript</title>
		<link>http://www.christatedavies.co.uk/2011/09/27/viewing-events-attached-to-elements-via-javascript/</link>
		<comments>http://www.christatedavies.co.uk/2011/09/27/viewing-events-attached-to-elements-via-javascript/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 15:32:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=243</guid>
		<description><![CDATA[If like myself, you often attach functions to elements using jQuery, for example: $('.btnName').live('click', function() { //do something }); There is a nice little browser extension that will show all the elements on the page with a little bubble outlining the attached events/functions. It is written by Allan Jardine, and you can get it from [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/09/27/viewing-events-attached-to-elements-via-javascript/' addthis:title='Viewing events attached to elements via JavaScript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>If like myself, you often attach functions to elements using jQuery, for example:</p>
<pre>$('.btnName').live('click', function() {
    //do something
});</pre>
<p>There is a nice little browser extension that will show all the elements on the page with a little bubble outlining the attached events/functions.</p>
<p>It is written by Allan Jardine, and you can get it from <a href="http://www.sprymedia.co.uk/article/Visual+Event" target="_blank">http://www.sprymedia.co.uk/article/Visual+Event</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/09/27/viewing-events-attached-to-elements-via-javascript/' addthis:title='Viewing events attached to elements via JavaScript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/09/27/viewing-events-attached-to-elements-via-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JQuery &#8211; bind event to non existant element</title>
		<link>http://www.christatedavies.co.uk/2011/05/10/jquery-bind-event-to-non-existant-element/</link>
		<comments>http://www.christatedavies.co.uk/2011/05/10/jquery-bind-event-to-non-existant-element/#comments</comments>
		<pubDate>Tue, 10 May 2011 10:29:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[click]]></category>
		<category><![CDATA[doesn't exist]]></category>
		<category><![CDATA[elements]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=162</guid>
		<description><![CDATA[We all have to bind events to elements: $('#link).click(function(){     do some stuff }); But, what if that element doesn&#8217;t exist yet? Well, you can use the &#8220;live&#8221; handler: $('#link').live('click', function(){     do some stuff }); This will carry the event handler on and will match any future element that matches the selector. More information [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/05/10/jquery-bind-event-to-non-existant-element/' addthis:title='JQuery &#8211; bind event to non existant element ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>We all have to bind events to elements:</p>
<pre>$('#link).click(function(){
    do some stuff
 });</pre>
<p>But, what if that element doesn&#8217;t exist yet? Well, you can use the &#8220;live&#8221; handler:</p>
<pre>$('#link').live('click', function(){
    do some stuff
 });</pre>
<p>This will carry the event handler on and will match any future element that matches the selector.</p>
<p>More information can be found here: <a href="http://api.jquery.com/live/" target="_blank">http://api.jquery.com/live/</a></p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/05/10/jquery-bind-event-to-non-existant-element/' addthis:title='JQuery &#8211; bind event to non existant element ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/05/10/jquery-bind-event-to-non-existant-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sorting a SELECT with JQuery</title>
		<link>http://www.christatedavies.co.uk/2010/10/04/sorting-a-select-with-jquery/</link>
		<comments>http://www.christatedavies.co.uk/2010/10/04/sorting-a-select-with-jquery/#comments</comments>
		<pubDate>Mon, 04 Oct 2010 12:36:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=114</guid>
		<description><![CDATA[Just a simple function to sort an already populated list. Obviously it would be easier to sort before populating, but there are the odd occasion where this is more complicated than you thought. Requirements : JQuery var selectOptions = $("#selectId option"); Then, we just run the sort command on the options array &#8211; and this [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/10/04/sorting-a-select-with-jquery/' addthis:title='Sorting a SELECT with JQuery ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Just a simple function to sort an already populated list. Obviously it would be easier to sort before populating, but there are the odd occasion where this is more complicated than you thought.</p>
<p>Requirements : JQuery</p>
<pre>var selectOptions = $("#selectId option");</pre>
<p>Then, we just run the sort command on the options array &#8211; and this easily sorts the items.</p>
<pre>selectOptions.sort(function(a, b) {
    if (a.text &gt; b.text) {
        return 1;
    }
    else if (a.text &lt; b.text) {
        return -1;
    }
    else {
        return 0
    }
});</pre>
<p>Then, at the end empty the select object and re-append the sorted options array</p>
<pre>$("#selectId").empty().append(selectOptions);</pre>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/10/04/sorting-a-select-with-jquery/' addthis:title='Sorting a SELECT with JQuery ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/10/04/sorting-a-select-with-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I wrote one of the first social networking sites</title>
		<link>http://www.christatedavies.co.uk/2010/05/13/i-wrote-one-of-the-first-social-networking-sites/</link>
		<comments>http://www.christatedavies.co.uk/2010/05/13/i-wrote-one-of-the-first-social-networking-sites/#comments</comments>
		<pubDate>Thu, 13 May 2010 09:13:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Access]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[fish-bar]]></category>
		<category><![CDATA[members]]></category>
		<category><![CDATA[social networking]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=73</guid>
		<description><![CDATA[Its not a claim to fame, or a preempt to a court case to a &#8216;big hitter&#8217;, but back in the year 2000, when I was a fledgling junior programmer, I wrote an ASP web site based around a bunch of &#8216;buddies&#8217;. You could add people to your list, message them, upload a picture (just [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/05/13/i-wrote-one-of-the-first-social-networking-sites/' addthis:title='I wrote one of the first social networking sites ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Its not a claim to fame, or a preempt to a court case to a &#8216;big hitter&#8217;, but back in the year 2000, when I was a fledgling junior programmer, I wrote an ASP web site based around a bunch of &#8216;buddies&#8217;. You could add people to your list, message them, upload a picture (just the one) of yourself, and view other&#8217;s profiles.</p>
<p>This sounds very familiar, right?</p>
<p><span id="more-73"></span></p>
<p>It was called the fish-bar, and even had a .com domain&#8230; www.fish-bar.com &#8211; at the time, there was a big thing about brand names, so I wanted a small name that people could remember and was loosely connected with friends, socialising, etc. So the &#8216;bar&#8217; was put into the name&#8230; The fish was just a random word&#8230; It worked well.</p>
<p>Unfortunately, the code was very clunky, and there was very little modular activity on it, but it worked, and for all its fault, security issues, etc, it was a working site and I had member (albeit only a dozen or so!)</p>
<p>The fish-bar used a dirty Microsoft Access database, and was built in  Microsoft Active Server pages. I said before that it was clunky, and it  was. It used very basic javascript, and there was absolutely no tests  for SQL injections, or scripting hacks&#8230; But, members could sign up for  free&#8230;</p>
<p>It went live in 2001 &#8211; and you can sort of see an archive of it at the way-back machine&#8230; (<a href="http://web.archive.org/web/*/http://www.fish-bar.com" target="_blank">http://web.archive.org/web/*/http://www.fish-bar.com</a>) &#8211; but it died a death in 2002 &#8211; but I can&#8217;t remember why. Perhaps I didn&#8217;t feel like renewing the domain name&#8230; Not sure&#8230;</p>
<p>It just goes to show, that anyone could&#8217;ve written a website that millions of people use, every day and get bought out by a major player with a team of developers and proper coding standards. If I&#8217;d kept up the coding and maintenance of the site it might have gone somewhere, or got me noticed by head hunters, or anything else.</p>
<p>We read stories of students selling their sites for millions of pounds, but I was not one of them.</p>
<p>I do still have some of the code that one of the guys at the wayback archive managed to send me, and I&#8217;ve uploaded it here, incase anyone wants to have a look and a laugh at how old and outdated it was. You can find the code <a href="http://www.christatedavies.co.uk/files/fishbar.rar">here</a></p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/05/13/i-wrote-one-of-the-first-social-networking-sites/' addthis:title='I wrote one of the first social networking sites ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/05/13/i-wrote-one-of-the-first-social-networking-sites/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Language Translation</title>
		<link>http://www.christatedavies.co.uk/2010/03/18/javascript-language-translation/</link>
		<comments>http://www.christatedavies.co.uk/2010/03/18/javascript-language-translation/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 01:48:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[google language]]></category>
		<category><![CDATA[translation]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=15</guid>
		<description><![CDATA[On a project I am working on, I need to enable language translation. Thinking about it, turned into something quite complicated. But then I found the Google Language API which allows developers to hook into the Google Languages services. So, I start a page &#8211; enabling UTF-8 character sets: &#60; meta content="text/html; charset=UTF-8" http-equiv="content-type"&#62; And [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/javascript-language-translation/' addthis:title='Javascript Language Translation ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>On a project I am working on, I need to enable language translation. Thinking about it, turned into something quite complicated. But then I found the Google Language API which allows developers to hook into the Google Languages services.</p>
<p>So, I start a page &#8211; enabling UTF-8 character sets:</p>
<pre>&lt; meta content="text/html; charset=UTF-8" http-equiv="content-type"&gt;</pre>
<p>And then hook into the Google API itself:</p>
<pre>&lt; script type="text/javascript" src="<a href="http://www.google.com/jsapi%22%3E%3C/script">http://www.google.com/jsapi"&gt;&lt;/script</a>&gt;</pre>
<p>Finally, a function that will pick up the text to translate. This function will automatically attempt to discover the source language for you.</p>
<pre>&lt; script type="text/javascript"&gt;
google.load("language", "1");

function translateLanguage() {
    var sourceText = document.getElementById("sourceText").value;
    google.language.detect(sourceText, function(lang) {
        if (lang.language != '') {
            document.getElementById('language').innerHTML = 'Source language: ' + lang.language;
            google.language.translate(sourceText, lang.language, 'en', function(result) {
                var translated = document.getElementById("translation");
                if (result.translation) {
                    translated.innerHTML = result.translation;
                }
            });
        }
    });
}
&lt;/script&gt;</pre>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/javascript-language-translation/' addthis:title='Javascript Language Translation ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/03/18/javascript-language-translation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

