<?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>klauskomenda.com &#187; Coding</title>
	<atom:link href="http://www.klauskomenda.com/archives/category/web/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.klauskomenda.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 14 Dec 2011 23:58:35 +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>Text to speech functionality in web applications using the iSpeech API</title>
		<link>http://www.klauskomenda.com/archives/2011/11/11/text-to-speech-functionality-in-web-applications-using-the-ispeech-api/</link>
		<comments>http://www.klauskomenda.com/archives/2011/11/11/text-to-speech-functionality-in-web-applications-using-the-ispeech-api/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 23:02:40 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[ispeech]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[text-to-speech]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=1067</guid>
		<description><![CDATA[Speech-enable your web applications using the iSpeech REST API. ]]></description>
			<content:encoded><![CDATA[<p>&#8220;Turn right onto Main Street&#8221;. If you are using a car navigation system like <a href="http://www.garmin.com/us/products/ontheroad">Garmin</a> or <a href="http://www.tomtom.com/">TomTom</a>, you will be all too familiar with phrases like this, spoken to you by the device mounted to your dashboard, magically guiding you through the maze of local roads and highways to your destination. Such <a href="http://en.wikipedia.org/wiki/Text_to_speech">text-to-speech</a> capabilities are usually associated with native software implementations, not so much with web applications. With the <a href="http://www.ispeech.org/developers">iSpeech API</a>, that changes. You can now build web applications that implement text-to-speech functionality with the power of JavaScript and HTML5.</p>
<p>The <a href="http://www.ispeech.org/text.to.speech.demo.php">iSpeech demo page</a> gives you an idea of the power of the API by providing a regular text input and being able to turn any text into spoken words. Let&#8217;s have a closer look now as to what is required to make this work in a web application.</p>
<h2>Step 1: Sign up for a Developer Account and set up an API key</h2>
<p><a href="http://www.ispeech.org/developer/signup/stage1">Signing up for a developer account</a> is a requirement in order to use the API. After creating your account and logging in, you can <a href="http://www.ispeech.org/developer/createkey">create a new API key</a>. Fill in the form and select &#8220;Desktop, Web, Other&#8221; as the application type in order to be able to use iSpeech&#8217;s REST API.</p>
<h2>Step 2: Trying out the API</h2>
<p>After creating an API key, you should find it in <a href="http://www.ispeech.org/developer/readallkeys">your list of available API keys</a>. Clicking on &#8220;Settings&#8221; leads you to a form where you can set certain parameters for the text-to-speech functionality, like file formats, bit rates and frequency as well as add some more information about the app that uses the key. For this step, we just want to try out the API and get some text converted into speech, so the first thing we need to do is familiarize ourselves with how the API request is constructed. iSpeech provides <a href="http://www.ispeech.org/api">good documentation</a> on that and gives us the general layout of the request:</p>
<pre><code>http://api.ispeech.org/api/rest?apikey=YOURAPIKEYHERE&amp;action=convert&amp;text=This+is+the+text+I+want+to+convert</code></pre>
<p>Now we only need to replace YOURAPIKEYHERE with the API key we created in Step 1 and copy that request URL to the address bar of our browser or create an <a href="http://html5doctor.com/native-audio-in-the-browser/">HTML5 audio element</a> like the one below to embed in our HTML document.</p>
<p><audio controls preload="auto" autobuffer><source src="http://api.ispeech.org/api/rest?apikey=developerdemokeydeveloperdemokey&#038;action=convert&#038;text=This+is+the+text+I+want+to+convert" /><source src="http://api.ispeech.org/api/rest?apikey=developerdemokeydeveloperdemokey&#038;action=convert&#038;text=This+is+the+text+I+want+to+convert&#038;format=ogg" /><source src="http://api.ispeech.org/api/rest?apikey=developerdemokeydeveloperdemokey&#038;action=convert&#038;text=This+is+the+text+I+want+to+convert&#038;format=wav" /></audio></p>
<h2>Step 3: Tweaking the Request Parameters</h2>
<p>The attentive reader might have noticed, from looking at the source code of the audio element in step 2, that we need to provide several fallback versions in addition to the MP3 format that the iSpeech API generates by default. This is necessary to satisfy the different codec requirements by modern browsers. To achieve this, the iSpeech API has a <span class="code">format</span> parameter, that lets us specify the format in which we would like the audio piece to be returned to us. A complete list of supported formats can be found in the <a href="http://www.ispeech.org/api">iSpeech API documentation</a>. </p>
<p>If we want to use the <a href="http://en.wikipedia.org/wiki/Ogg_vorbis">Ogg Vorbis</a> format, we can specify this like this:</p>
<pre><code>http://api.ispeech.org/api/rest?apikey=YOURAPIKEYHERE&amp;action=convert&amp;text=This+is+the+text+I+want+to+convert&amp;format=ogg</code></pre>
<p>We can also specify a different voice using the &#8220;voice&#8221; parameter:</p>
<pre><code>http://api.ispeech.org/api/rest?apikey=YOURAPIKEYHERE&amp;action=convert&amp;text=This+is+the+text+I+want+to+convert&amp;format=ogg&amp;voice=auenglishfemale</code></pre>
<p>It is also possible to slow down the voice by providing a &#8220;speed&#8221; parameter with values between -10 (very slow) and 10 (very fast). However I noticed that not all voices provided by iSpeech support this parameter.</p>
<pre><code>http://api.ispeech.org/api/rest?apikey=YOURAPIKEYHERE&amp;action=convert&amp;text=This+is+the+text+I+want+to+convert&amp;format=ogg&amp;speed=-5</code></pre>
<h2>Sounds great (literally), but&hellip;</h2>
<p>By this point it is probably obvious that being able to use this text-to-speech API from within HTML documents using regular web technologies is pretty powerful. However, there is a catch to it in the name of costs. The web API key that we created is free for the first 500 words being transformed to audio by the API (you can check your key&#8217;s total word balance on the settings page of the key). If you exceed that limit, you have to pay, $50 for 1000 word credits to be exact, which essentially means that you can only feasibly use this API in some sort of premium app setting where your revenue generated can cover for these costs. While I would love this technology to be free, I can understand that it is not, considering the amount of work that goes into developing such piece of software. </p>
<p>The upside is, however, that iSpeech in addition to the REST API also offers SDKs for pretty much all popular mobile platforms, such as the <a href="http://www.ispeech.org/developers/iphone">iPhone</a>, and API keys generated for these do not have any word quota imposed on them. Maybe another good reason to start learning Objective-C.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2011/11/11/text-to-speech-functionality-in-web-applications-using-the-ispeech-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Search along a route with CloudMade&#8217;s Local Search API</title>
		<link>http://www.klauskomenda.com/archives/2011/11/06/search-along-a-route-with-cloudmades-local-search-api/</link>
		<comments>http://www.klauskomenda.com/archives/2011/11/06/search-along-a-route-with-cloudmades-local-search-api/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 05:09:49 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[bikenav]]></category>
		<category><![CDATA[cloudmade]]></category>
		<category><![CDATA[local search]]></category>
		<category><![CDATA[search along route]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=1065</guid>
		<description><![CDATA[A tutorial demonstrating how to give users functionality that they were used to get only from their car navigation systems.]]></description>
			<content:encoded><![CDATA[<p>Suppose you go on a casual bike ride with a couple of friends. Wouldn&#8217;t it be nice to know if there are any coffee shops located along the route to refuel on caffeine and sugar? Or lets say you are doing a longer ride and would want to pick up a spare tube along the way. It would be good to know if there is any bike shop conveniently located along the route. Meet <a href="http://developers.cloudmade.com/projects/local-search/examples/searching-for-objects-along-route">CloudMade&#8217;s Local Search API</a>, which I used to power this specific feature in the most recent version of <a href="/apps/bikenav/">BikeNav</a>. This article is going to show you how you can include that in your next mobile-aware app.</p>
<h2>Step 1: Sign up for a Developer Account and get an API key</h2>
<p>CloudMade lets you <a href="http://cloudmade.com/register">sign up for a developer account</a> for free. After completing that initial step, access your account and click on the &#8220;Get API Key&#8221; button. Fill the details into the form presented to you, selecting &#8220;Web&#8221; as the platform and the &#8220;Free&#8221; pricing plan. Since we will not use any of the <a href="http://cloudmade.com/select/web">other features</a> the API provides, the free version has more than we need.</p>
<h2>Step 2: Create a token from your API key</h2>
<p>In order to be able to communicate with CloudMade&#8217;s servers, you need to create an access token from your API key. This is done by sending an HTTP POST request to a particular authentication server as noted in <a href="http://developers.cloudmade.com/projects/show/auth">their documentation</a>. If you are using PHP, the following code can help you out (using <a href="http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/">Wez Furlong&#8217;s do_post_request function</a>).</p>
<pre><code>$apiKey = 'my-api-key';
$userId = 'your-generated-user-id';

// http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' =&gt; array(
              'method' =&gt; 'POST',
              'content' =&gt; $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}
$token =  do_post_request('http://auth.cloudmade.com/token/' . $apiKey . '?userid=' . $userId, '');

echo "generated token: " . $token;</code></pre>
<h2>Step 3: Requesting POI data along a given route</h2>
<p>Equipped with the token from Step 2, we can now try to retrieve POIs along the route. The following example would do a search along the route for a maximum of 20 points of interest of type &#8220;cafe&#8221; from my place to Cupertino, within a radius of 1 kilometer and returning the data in <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> format.</p>
<pre><code>http://geocoding.cloudmade.com/[APIKEY]/geocoding/v2/find.js?token=[TOKEN]&amp;object_type=cafe&amp;along=37.36721,-122.03533,37.36756,-122.0354,37.36752,-122.03662,37.36672,-122.03607,37.36642,-122.03648,37.36429,-122.03288,37.33911,-122.03241,37.33763,-122.03239,37.32283,-122.03241&amp;distance=1000&amp;results=20&amp;callback=mySuperCallback</code></pre>
<p>The <a href="http://developers.cloudmade.com/wiki/geocoding-http-api/Documentation">find method</a> of the Local Search API lets you pass in various parameters, which are <a href="http://developers.cloudmade.com/wiki/geocoding-http-api/Documentation#Parameters">well documented</a>. For the purpose of this article, the following are the most important ones:</p>
<ul>
<li><span class="highlight">object_type</span>: the API lets you choose from an <a href="http://developers.cloudmade.com/wiki/geocoding-http-api/Object_Types">extensive list of point-of-interest types</a>
</li>
<li><span class="highlight">along</span>: vector of the route your are traveling along, i.e. a comma separated list of lat/long pairs</li>
<li><span class="highlight">distance</span>: radius of the search are in meters</li>
<li><span class="highlight">results</span>: number of results to return</li>
<li><span class="highlight">callback</span>: signaling the server to return the response in JSONP format and specifying a publicly accessible JavaScript function to execute, passing in the response data</li>
</ul>
<p>The result from the above request will come back as JSONP and look similar to the following:</p>
<pre><code>mySuperCallback({
    "found": "3",
    "bounds": [{
        "json": ["37.33155", "-122.03642"]
    }, {
        "json": ["37.36805", "-122.03266"]
    }],
    "features": [{
        "id": "7367785",
        "centroid": {
            "type": "POINT",
            "coordinates": ["37.36768", "-122.03642"]
        },
        "bounds": [{
            "json": ["37.36768", "-122.03642"]
        }, {
            "json": ["37.36768", "-122.03642"]
        }],
        "properties": {
            "operator": "Starbucks",
            "osm_element": "node",
            "amenity": "cafe",
            "synthesized_name": "Cafe",
            "osm_id": "266625630"
        },
        "type": "Feature"
    }, {
        "id": "26073213",
        "centroid": {
            "type": "POINT",
            "coordinates": ["37.33155", "-122.03266"]
        },
        "bounds": [{
            "json": ["37.33155", "-122.03266"]
        }, {
            "json": ["37.33155", "-122.03266"]
        }],
        "properties": {
            "osm_element": "node",
            "amenity": "cafe",
            "name": "Bagel Street Cafe",
            "osm_id": "957085286"
        },
        "type": "Feature"
    }, {
        "id": "14342531",
        "centroid": {
            "type": "POINT",
            "coordinates": ["37.36805", "-122.03338"]
        },
        "bounds": [{
            "json": ["37.36805", "-122.03338"]
        }, {
            "json": ["37.36805", "-122.03338"]
        }],
        "properties": {
            "osm_element": "node",
            "amenity": "cafe",
            "name": "Peet's Coffee",
            "osm_id": "441987522"
        },
        "type": "Feature"
    }],
    "type": "FeatureCollection",
    "crs": {
        "type": "EPSG",
        "properties": {
            "code": "4326",
            "coordinate_order": ["0", "1"]
        }
    }
});</code></pre>
<p>In our <span class="code">mySuperCallback</span> function we can then use this data to e.g. plot each of the returned points of interest on a map, in addition to the given route. </p>
<h2>Be aware of long routes</h2>
<p>Attentive readers will notice that there is a slight flaw in the way the request is constructed. If you have a long route with a lot of waypoints that you are passing along in the request to the search API, at some point you will reach the limit of the maximum amount of characters you can pass along in a URL. One way to mitigate this is to drop every other entry from the array of waypoints if the number exceeds a certain limit. That way, the search will still be somewhat accurate (since you are only taking out points in between instead of cutting them off at the beginning or the end). A JavaScript example code could look like this:</p>
<pre><code>maxWaypoints = 160; // a good threshold that I noticed works well for BikeNav
waypoints = [ ... ]; // array where each entry represents a waypoint along the route
no_of_waypoints = waypoints.length;

if (no_of_waypoints &gt; maxWaypoints) {
    while (waypoints.length &gt; maxWaypoints) {
        for (var i = 0, len = waypoints.length; i &lt; len; i++) {
            waypoints.splice(i + 2, 1);
        }
    }
}</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2011/11/06/search-along-a-route-with-cloudmades-local-search-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First Steps with YUI3</title>
		<link>http://www.klauskomenda.com/archives/2009/07/20/first-steps-with-yui3/</link>
		<comments>http://www.klauskomenda.com/archives/2009/07/20/first-steps-with-yui3/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 19:03:40 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=781</guid>
		<description><![CDATA[YUI3 moved from "Preview Release" to "Beta" a few weeks ago. So I figured, this is a good time now to play around with it and see what my colleagues on the other side of the wall came up with.]]></description>
			<content:encoded><![CDATA[<p>I have used <a href="http://developer.yahoo.com/yui/">YUI2</a> quite extensively since I joined Yahoo! in May 2007. Before that I did not really have much experience with any JavaScript library, I had played around with <a href="http://script.aculo.us/">script.aculo.us</a> and <a href="http://jquery.com/">jQuery</a> a little bit, but that was all, pretty much. Because here at Yahoo! we, naturally, use YUI in all our projects, I had to dive in and learn how to use it. After a bit of a rough start, figuring out the basics, I started to like YUI2. The reasons why were the modular approach, the good API documentation and, to me, the general way of how to use certain functions seemed pretty logical to me. Now, with the <a href="http://developer.yahoo.com/yui/3">new 3-series</a> coming out, I was curious about what changed, what improved and what to learn in order to use the next generation of the library.</p>
<h2>A few major differences</h2>
<p>I am not going to go into all the things that are different between YUI2 and YUI3, this is beyond the scope of this post. There is already lots of documentation on <abbr title="Yahoo! Developer Network"><a href="http://developer.yahoo.com/yui/3">YDN</a></abbr> and a <a href="http://developer.yahoo.com/yui/theater/desai-yui3.html">very good video</a> covering lots of questions like &#8220;So whats different?&#8221;. Just a few key things to note:</p>
<ul>
<li>YUI3 is majorly different to YUI2. It is basically a new library and thus also, in a lot of ways, works completely different than its predecessor.</li>
<li>To avoid conflicts between the two libraries, the global YAHOO object (used in YUI2) is now YUI. This also allows to have both YUI2 and YUI3 on the same page.</li>
<li>Every piece of code you write needs to be wrapped in the following statement: <code>YUI().use([YUI3 modules I want to use], function (Y) { ...my code here... } );</code>This, in theory, makes creating namespaces, like we did in YUI2, unnecessary.</li>
</ul>
<h2>A Simple Example: JavaScript Countdown</h2>
<p>Just to play around with it and start with a pretty basic example, I wanted to do a countdown in JavaScript. How would I do that using YUI3?</p>
<h3>Step by Step</h3>
<p>The following steps would need to get executed in order to make this example work:</p>
<ol>
<li>Put basic markup on the page, which marks the start time of the countdown, e.g. 30 minutes and 0 seconds.</li>
<li>Have one function, <span class="code">decreaseSeconds</span> that decreases the seconds and call this function every second (or 1000 milliseconds).</li>
<li>If the seconds counter reaches 0, decrease the minutes using <span class="code">decreaseMinutes</span>.</li>
<li>Stop the timer if minutes and seconds reach 0.</li>
</ol>
<h3>Basic Markup</h3>
<p>As said, the markup on the page already serves as the start time for the countdown and is pretty simple:</p>
<pre><code >&lt;div id="countdown"&gt;
        &lt;span id="min"&gt;30&lt;/span&gt;:&lt;span id="sec"&gt;00&lt;/span&gt;
&lt;/div&gt;</code></pre>
<h3>Putting YUI3 on the page</h3>
<p>In order to put YUI3 on the page, we need to include the YUI Global Object:</p>
<pre><code>&lt;script src="http://yui.yahooapis.com/3.0.0b1/build/yui/yui-min.js" type="text/javascript"&gt;&lt;/script&gt; </code></pre>
<p>And just to verify this is working, we will add some debugging code on the page:</p>
<pre><code>YUI().use("node", function (Y) {
    alert("YUI3 is here!");
});</code></pre>
<p>Just a few words on this. The first argument of the <span class="code">use</span> function, in this case &#8220;node&#8221;, tells the YUI instance which modules to load. As it will turn out later, the <a href="http://developer.yahoo.com/yui/3/node/">node module</a>, together with the functionality that the YUI global object provides, are enough to make this example work. The last argument here is a callback function, which has the YUI instance as an argument. In this function is where your code lives and has access to the YUI3 instance and modules, if loaded in. More on this in the documentation.</p>
<p>So we got the <a href="/lab/yui3/countdown/basics.html">basics</a> covered, YUI3 is on the page and we can move on to the program logic.</p>
<h3>Setting up variables and functions</h3>
<p>First I am going to set up some variables that I am going to need throughout the code:</p>
<pre><code>var minutes = Y.get("#min"),
    seconds = Y.get("#sec"),
    timer,
    liftOff = false;</code></pre>
<p>As you can see here, YUI3 gives you the ability to access a DOM element using CSS selectors out of the box. <span class="code">Y.get(&#8220;#min&#8221;)</span> returns a node (in YUI3 terms) which wraps the underlying DOM object. But be aware that this object here is <em>not</em> the DOM object itself, which is different to what <span class="code">YAHOO.util.Dom.get(&#8220;min&#8221;)</span> would do. </p>
<p>We will also set up the <span class="code">decreaseSeconds</span> function and the timer:</p>
<pre><code>function decreaseSeconds() {
    // get current seconds value
    var secs = seconds.get("innerHTML");

    if (secs === "00") {
        // move to 59
        secs = 59;
        decreaseMinutes();
    } else {
        secs--;
        if (secs &lt; 10) {
            secs = "0" + secs;
        }
    }

    if (!liftOff) {
        seconds.set("innerHTML", secs);
    }
}

timer = Y.later(1000, null, decreaseSeconds, [], true);</code></pre>
<p>Instead of talking about the program logic, which should be pretty straight forward, I would like to highlight YUI3 specific things here:</p>
<ul>
<li>Setting up the timer using <span class="code">Y.later</span> is pretty easy and comparable to <span class="code">YAHOO.later</span> in YUI2.</li>
<li>As the node returned by <span class="code">Y.get</span> is not the DOM node, doing something like <span class="code">seconds.innerHTML</span> does not work. In fact, it will return &#8216;undefined&#8217;. If you do a <span class="code">console.dir(seconds)</span> you will discover that this does not look like a DOM object at all, but is, as stated above, a YUI node instance wrapping the DOM element with id=&#8221;sec&#8221;. So instead of doing <span class="code">.innerHTML</span>, we need to use <span class="code">seconds.get(&#8220;innerHTML&#8221;)</span> to access the content between the opening and closing tag.</li>
<li>Likewise, to set content using innerHTML, we need to do <span class="code">seconds.set(&#8220;innerHTML&#8221;, secs)</span>.</li>
</ul>
<p>This is something to get used to at first. Because in YUI2, people (including me) were used to have the actual DOM object at hand and perform native DOM operations on it. <del>This is not possible anymore with YUI3</del>. It is still possible to access the underlying DOM objects, see <a href="/archives/2009/07/20/first-steps-with-yui3/#comment-22211">Luke&#8217;s comment</a>.</p>
<p>After <a href="/lab/yui3/countdown/vars_and_funct.html">setting this up</a>, we can move on to adding the functionality for decreasing the minutes and making the countdown work.</p>
<h3>Making it work</h3>
<p>Here is the code and logic for decreasing the minutes and canceling the timer if 0 is reached:</p>
<pre><code>function decreaseMinutes() {
    var mins = minutes.get("innerHTML");

    if (mins &gt; 0) {
        mins--;
        if (mins &lt; 10) {
            mins = "0" + mins;
        }
        minutes.set("innerHTML", mins);
    } else {
        timer.cancel();
        liftOff = true;
    }
}</code></pre>
<p>Pretty straight forward and also uses the wrapper methods to access DOM functionality. <a href="/lab/yui3/countdown/almost_there.html">It works now</a>, but we are not quite finished yet.</p>
<h3>Modularizing</h3>
<p>As you can see, all the code is currently inline on the page and gets executed as soon as the page loads. This is suboptimal. What I used to do using YUI2 is creating a module specific namespace and then call a public method (made available through the use of the Module Pattern) on the page that needed that functionality. So for this the module would be, e.g.:</p>
<pre><code>YAHOO.Klaus.Countdown = function () {
    ... my code ...
    return {
         init: init
    };
}();</code></pre>
<p>And then, on whatever page I want to use it, do:</p>
<pre><code>YAHOO.Klaus.Countdown.init();</code></pre>
<p>I was curious how to achieve something like this in YUI3. </p>
<p>I came up with a solution but I am not sure if this is the suggested way of doing this. I hope it is, as I couldn&#8217;t find a different way of doing it. The secret is to add the &#8220;module&#8221; you created as a module to YUI3, like:</p>
<pre><code>Y.add("Klaus.Countdown", init);</code></pre>
<p>The second argument is a function name to be executed when the module is being invoked used the <span class="code">use()</span> function.</p>
<p>So what is left to do is saving the countdown code to a separate file and then call the module from within a script block on the page:</p>
<pre><code>&lt;script type="text/javascript"&gt;
    YUI().use("Klaus.Countdown");
&lt;/script&gt;</code></pre>
<p>So we are done and hopefully happy with our <a href="/lab/yui3/countdown/countdown.html">first steps</a> in YUI3. </p>
<h2>Conclusion</h2>
<p>I have deliberately chosen a simple example to start with to avoid frustrations coming out of having to learn and getting used to too many things at once. Like with every new piece of software, getting started &#8220;somehow&#8221; in the first place is critical and finding out how to achieve things without running into too many roadblocks at once. How easy or hard it is to use YUI3, I have yet to determine when probably facing more complex examples, but I believe this simple examples gives a sneak peak into what awaits at the end of the line.</p>
<h2>Resources</h2>
<ul>
<li><a href="http://developer.yahoo.com/yui/3/">YUI3 on YDN</a></li>
<li><a href="http://developer.yahoo.com/yui/3/api/">YU3 API Doc</a></li>
<li><a href="http://yuiblog.com/">YUI Blog</a></li>
<li><a href="http://www.fatcow.com/edu/first-steps-with-yui3-be/">Belorussian translation of this article</a> kindly provided by Patricia Clausnitzer and <a href="http://www.fatcow.com/">http://www.fatcow.com/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2009/07/20/first-steps-with-yui3/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Book Review: Object-Oriented JavaScript</title>
		<link>http://www.klauskomenda.com/archives/2009/04/25/book-review-object-oriented-javascript/</link>
		<comments>http://www.klauskomenda.com/archives/2009/04/25/book-review-object-oriented-javascript/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 16:07:34 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=752</guid>
		<description><![CDATA[If you ever wanted a JavaScript book that talks about intermediate to advanced, cutting-edge and state-of-the-art techniques, this is the one. Your newly acquired knowledge might also get you nice looks from all the ladies at the next YUI party. Who knows.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book/mid/030309lf8ofn"><img src="/wp-content/uploads/2009/03/oo_js_cover.jpg" width="235" height="298" class="img-right" /></a>It has been a while since the late nineties in which JavaScript was (ab)used in a very hacky way, i.e. having to work around browser vendor proprietary code (Internet Explorer and Netscape) and, because it provided such a nice variation to otherwise static pages, adding the most useless features to a webpage. Anyone remember the snowflakes following the mouse cursor? Quite some time passed since then and thankfully, JavaScript grew out of puberty and has, in recent years, reached a quite significant stage of adulthood.</p>
<p>Stoyan Stefanov, a colleague of mine at <a href="http://yahoo.com">Yahoo!</a>, recently published <a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book/mid/030309lf8ofn">Object-Oriented JavaScript</a>. Personally, the book about JavaScript I was waiting for. A while back, I read and reviewed Jeremy Keith&#8217;s <a href="/library/jeremy-keith/dom-scripting-web-design-with-javascript-and-the-document-object-model/">DOM Scripting</a>, which is the perfect introduction as to how developers should use JavaScript these days in a browser environment. Progressive Enhancement was one of the key concepts in there. So after getting the basics covered, my expectation was that Stoyan&#8217;s book provides me with supporting material for intermediate to advanced JavaScript programming tasks. Does it deliver? We will come to that very shortly.</p>
<h2>Contents</h2>
<p>Chapters 1 to 4 in his book cover a brief history about JavaScript and the basic elements of the language, like variables, primitive data types, conditions and loops as well as functions and objects. The latter already have their specialties in JavaScript and Stoyan explains those special characteristics. </p>
<p>Chapters 5 and 6 are devoted to prototype and how inheritance works in JavaScript, a very essential thing to cover when we are talking about advanced techniques. </p>
<p>Chapter 7 talks about what readers of DOM Scripting might still remember, namely how JavaScript can be used to access and manipulate specific elements in the DOM. The author also takes a look at Events and the ever popular XMLHttpRequest.</p>
<p>And finally, the highlight comes last, Chapter 8 deals with what hardcore programmers were waiting for: Coding and Design Patterns. <a href="/code/javascript-programming-patterns/">Having done a little research about this myself</a>, I found it great that Stoyan documents concepts that have become good practice in the JavaScript world, like namespacing and creating public and private properties and methods. In the design patterns part, he talks about Singletons amongst various other patterns at the programmers disposal. </p>
<p>The book closes with a quite comprehensive appendix, which includes listing reserved words, referencing built-in functions and objects and regular expressions in JavaScript. </p>
<h2>Rating and Reasoning</h2>
<h3 title="My Rating: 9 out of 10" class="rating rating-level-9">My Rating: 9 out of 10</h3>
<p>Even though it states that &#8220;no prior JavaScript knowledge is required&#8221;, I would say that is a bit of an understatement. If DOM Scripting was your first JavaScript book then I would put the following equation on the board: DOM Scripting + experience with these techniques in The Real World (meaning on actual live projects) = ready for Object-Oriented JavaScript. Don&#8217;t get me wrong, Stoyan covers the basics (variables, conditions etc.) in the first few chapters, but after that, the learning curve gets pretty steep and I believe it is safe to say, without <em>any</em> prior knowledge, you will put this book back on the shelf, left frustrated. </p>
<p>I have worked on a couple of minor web projects before <a href="http://uk.yahoo.com">Yahoo! Europe</a> hired me in May 2007, so to this date, I believe I was able to acquire some experience when it comes to using JavaScript on high-scale websites. From my perspective, the first couple of chapters was a nice refresh on the topic, but only then, in Chapters 5 to 8, things got interesting with bits and pieces in there that helped me closing some knowledge gaps about certain concepts, which is really great. This, again, supports my feeling that this book is not meant for <em>every</em> JavaScript developer out there, independent of whether it is a newbie or an advanced developer. I feel that people with probably a couple of years experience might get more out of it than someone who just delved into JavaScript a month ago. </p>
<p>It is difficult to come up with concrete examples to explain this, and this observation is highly subjective of course, but I feel that the way the book is done and written, it feels a lot &#8220;drier&#8221; and less &#8220;exciting&#8221; to read compared to DOM Scripting. Which, again, if this targets JavaScript beginners, should not be the case. But I believe the more complex the material gets, the harder the job becomes of having to thrill and excite the reader. </p>
<p>What I really like about the book is that basically all the examples Stoyan gives, you can type into the Firebug console right away to try out for yourself. As I am a believer that you learn new skills most effectively by actually <em>doing</em> something and play with it, I am pretty sure this is a good way of imparting knowledge. </p>
<h2>Conclusion</h2>
<p>If you are a Web Developer with several years of experience on the job and with JavaScript and you want to move your knowledge level up a notch or two, this book is for you. If you want to play with the cool kids, then <a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book/mid/030309lf8ofn">get this book</a>. If you are more or less new to the JavaScript world, you might want to consider starting with DOM Scripting first and getting experience using those techniques on real world projects, before diving into the advanced concepts discussed in Object-Oriented JavaScript.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2009/04/25/book-review-object-oriented-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CSS Reviewr</title>
		<link>http://www.klauskomenda.com/archives/2008/12/20/css-reviewr/</link>
		<comments>http://www.klauskomenda.com/archives/2008/12/20/css-reviewr/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 20:17:58 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=740</guid>
		<description><![CDATA[Check your CSS code against certain standards and best practices. Brought to you by Santa Klaus. Merry Christmas. ]]></description>
			<content:encoded><![CDATA[<p>There is <a href="http://www.jslint.com/">JSLint</a> for JavaScript, <a href="http://pear.php.net/package/PHP_CodeSniffer">PHP_CodeSniffer</a> for PHP&#8230;why not have some sort of code checking tool for CSS? Based on the outcome of several code reviews we had at <a href="http://uk.yahoo.com/">Yahoo! Europe</a>, I thought that it would come in handy having a tool that would catch findings automatically. So over the last couple of months I was working on such a tool and called it <a href="http://www.klauskomenda.com/tools/cssreviewr/">CSS Reviewr</a>.</p>
<p><a href="http://www.klauskomenda.com/tools/cssreviewr/"><img alt="Try out CSS Reviewr" src="http://www.klauskomenda.com/wp-content/uploads/2008/12/cssreviewr_logo.gif" alt="" width="291" height="50" class="img-right" /></a>It is quite similar to a couple of tools that are already out there. JSLint looks for issues in JavaScript code and checks for adherence to a certain set of code standards. PHP_CodeSniffer, a <a href="http://pear.php.net/">PEAR</a> package, provides similar functionality for PHP files (but is also able to check JavaScript and CSS). Then there is <a href="http://csstidy.sourceforge.net/">CSSTidy</a>, a parser and optimiser for CSS, which e.g. compresses hex values for color declarations and provides different levels of minification (removing comments, whitespace etc.). </p>
<p>Essentially, what I wanted to achieve with CSS Reviewr is to not only point out issues in the code based on the experiences and the reviews we conducted at work, but also to provide explanatory text for each finding (much like how the <a href="http://validator.w3.org/">W3C Validator</a> does it for HTML). In one or two sentences, I wanted to explain why this is an issue and how it can be resolved. From my perspective, this is ideal for a developer who might not be familiar with a certain best practice or convention. By reading the explanation and maybe following a link to a page that provides more details, it has a certain learning effect to it. </p>
<p>CSS Reviewr, as it is stated on the page, is <em>not</em> a validator. It is encouraged to run your code through the <a href="http://jigsaw.w3.org/css-validator/">W3C CSS Validator</a> first, before checking it using CSS Reviewr.</p>
<p>Last, but not least, I would like to thank the real brain behind this: <a href="http://samriley.net/">Sam Riley</a>. I had the pleasure of working with him on <a href="http://answers.yahoo.com/">Yahoo! Answers</a> and I was and am still really impressed by his sheer endless knowledge of the ins and outs of CSS. Knowing that I would not be able to just turn to the side and ask him whenever I have a question related to CSS (because I relocated from London to California), I wrote this tool also to store his CSS knowledge somewhere for myself, so I could still use it here in the US <img src='http://www.klauskomenda.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  So a big thanks to Sam!. Also thanks to <a href="http://www.evalotta.net/">Eva-Lotta</a>, who kindly offered to do some visual design work for this in her spare time. </p>
<p>The tool is in beta right now and I have already identified <a href="http://www.klauskomenda.com/tools/cssreviewr/package/BUGS">some bugs</a> myself that I need to fix at some point, providing I find time for this. Getting some early feedback from colleagues, I have also received the following feature requests:</p>
<ul>
<li>Indentation check (selectable option)</li>
<li>Check (background) images used in the code are akamized/on a content delivery network (CDN)</li>
<li>Fonts should not use uppercase characters (e.g &#8220;Arial&#8221; should be &#8220;arial&#8221;)</li>
<li>Limit the amount of errors shown to 50 to prevent browser (FF2) from crashing. Add something like: &#8220;More than 50 issues found. Fix the issues above first before checking your code again.&#8221;</li>
<li>Having a &#8216;Do it!&#8217; button by each issue to make the suggested modification to the code, and then have a textarea at the bottom where you could copy out the fixed version</li>
</ul>
<p>Suggestions, bug reports and all kinds of feedback welcome. Just leave a comment below. Thanks and happy holidays!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2008/12/20/css-reviewr/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>WordPress Plugin: Shrinkr</title>
		<link>http://www.klauskomenda.com/archives/2008/10/22/wordpress-plugin-shrinkr/</link>
		<comments>http://www.klauskomenda.com/archives/2008/10/22/wordpress-plugin-shrinkr/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 19:55:20 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=683</guid>
		<description><![CDATA["More power!", as Tim Taylor from the TV sitcom "Home Improvement" would say. Don't we all want more power, more performance from our web sites? I was working on a WordPress plugin that enables you to bump up the speed of your site by minification of your CSS and JavaScript files. And now finally found time to release it.]]></description>
			<content:encoded><![CDATA[<p>With higher expectations, both in terms of design and interaction of websites, which are more like web applications these days, the amount and size of JavaScript and CSS files used have increased dramatically during the last couple of years. Ajax, Drag &amp; Drop, Animation&#8230;if your site doesn&#8217;t keep up with the latest trend, you might be left out. But bigger file sizes (and more files) also mean more HTTP requests and having to download more data from the server over an Internet connection, which means slower loading of the page. <a href="http://www.klauskomenda.com/code/wordpress-plugin-shrinkr/">Shrinkr</a> helps you to tackle that problem.</p>
<h2>Description</h2>
<p>Shrinkr does two things:</p>
<ol>
<li>It reduces the file-size of your CSS and JavaScript files through <a href="http://en.wikipedia.org/wiki/Minify">minification</a>, i.e. stripping out unnecessary content like white-space and comments</li>
<li>It then takes those files and concatenates them into one single CSS and one single JavaScript file</li>
<li>Those files are then getting stored as css-min-[timestamp].css and js-min-[timestamp].js on your server. The reason for the timestamp is that whenever Shrinkr creates a new file, it is ensured that a user agent doesn&#8217;t still use a cached version of the previous files but, because the name of the file has changed, retrieves the latest version from the server</li>
</ol>
<p>The first measure reduces the file-size by stripping out things from files that are not needed by the browser in order to interpret them. Whitespace and comments are for developers, not for the browser. They can safely be removed, which reduces the file-size and thus the consumption of bandwidth.</p>
<p>In the <a href="http://developer.yahoo.com/performance/rules.html">Yahoo! Exceptional Performance Rules</a>, it says that one should aim to <a href="http://developer.yahoo.com/performance/rules.html#num_http">reduce the number of HTTP requests</a> in order to make a page load as fast as possible. Shrinkr does that by concatenating all the files into one single file (one for CSS and one for JS). So instead of having 10 CSS files, you will end up having only one.</p>
<p>Shrinkr also adds an action to wp_head and wp_footer in order to tell WordPress to include those files upon page load in the appropriate section of the page. <a href="http://developer.yahoo.com/performance/rules.html#css_top">CSS files should be included in the head of the page</a>, <a href="http://developer.yahoo.com/performance/rules.html#js_bottom">JS at the very bottom</a> as per the Yahoo! Exceptional Performance Rules. </p>
<p>Because of the fact that you will end up having all your CSS and JavaScript in one file and on one line (because all the carriage returns etc. have been stripped out), it makes debugging kind of difficult. For that reason, you should only activate that plugin on the live version of your site and keep the files separated on your development box.</p>
<p>Further details about the plugin can be found on the <a href="http://www.klauskomenda.com/code/wordpress-plugin-shrinkr/">Shrinkr plugin page</a>. It would be great if you could try it out and give me feedback, either by leaving a comment here or using the <a href="/contact/">contact form</a>. Thanks!</p>
<h2>Resources</h2>
<ul>
<li><a href="http://developer.yahoo.com/performance/rules.html">Best Practices for Speeding Up Your Web Site</a></li>
<li><a href="http://developer.yahoo.com/yui/compressor/">Yahoo! UI Library: YUI Compressor</a></li>
<li><a href="http://www.crockford.com/javascript/jsmin.html">JSMIN, The JavaScript Minifier</a></li>
<li><a href="http://code.google.com/p/minify/">Minify!</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2008/10/22/wordpress-plugin-shrinkr/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Favicons&#8212;demystified</title>
		<link>http://www.klauskomenda.com/archives/2008/10/07/faviconsdemystified/</link>
		<comments>http://www.klauskomenda.com/archives/2008/10/07/faviconsdemystified/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 20:45:34 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=622</guid>
		<description><![CDATA[Once for every project you do you stumble upon them: favicons. And every time I struggle to remember what it is I need to pay attention to when creating them. So here is another guide for the next time I come across the to-do item "Create a favicon". ]]></description>
			<content:encoded><![CDATA[<p>One of the few things you only come across once in a while is creating a <a href="http://en.wikipedia.org/wiki/Favicon">favicon</a>. It only needs to be done once, doesn&#8217;t need any ongoing maintenance or debugging. Do it once, done. That&#8217;s the reason, at least in my case, that every time I get to the task &#8220;Create a favicon for the page&#8221;, I try to remember how exactly they need to be created and what to pay attention to. So this is another &#8220;remember for next time&#8221; article. </p>
<p>There are three ways to generate a favicon:</p>
<ul>
<li>Photoshop (or any other graphics editing program)</li>
<li>Online tools</li>
<li>On the fly</li>
</ul>
<h2>Photoshop</h2>
<p>I say Photoshop here, because that is what I am used to, but essentially you could use any graphics editing program out there. Essentially, when you have your graphic ready, you could just simply save it as a PNG or GIF. Well, you can do that, but that won&#8217;t work in Internet Explorer. </p>
<p>To make your favicon display in every browser, you need to create an <a href="http://en.wikipedia.org/wiki/ICO_(icon_image_file_format)">ICO</a> file. To do that with Photoshop, you can install a plugin that helps you do so. </p>
<h3>Installing the plugin</h3>
<p>First you need to download the plugin from <a href="http://www.telegraphics.com.au/sw/">Telegraphics</a> for whichever version of Photoshop you are using. Extract the ZIP file and move the plugin file into the &#8220;File Formats&#8221; folder inside your Photoshop Plugins folder:</p>
<ul>
<li>On Windows, ICOFormat.8bi (the path should be similar to: C:\Program Files\Adobe\Adobe Photoshop CS2\Plug-Ins\File Formats\)</li>
<li>On OS X/Classic, icoformat (CS2/Mac version is ICOFormat_cs2.plugin)</li>
</ul>
<p>After you have done that quit and relaunch Photoshop, if it&#8217;s already running. To check if the plugin is installed correctly, go to Help &gt; About Plug-ins. In this list, the ICO plugin should appear.</p>
<h3>Saving your graphic in ICO format</h2>
<p>There are some constraints as to which images are eligible to be saved in ICO format:</p>
<ul>
<li>The ICO format does not allow images more than 255 pixels high or wide.</li>
<li>Only Bitmap, Grey Scale, Indexed and RGB mode images, no more than 8 bits per channel, can be saved as ICO.</li>
</ul>
<p>If the graphic you created meets those requirements, you can go to &#8220;Save&#8221; or &#8220;Save As&#8230;&#8221; and select &#8220;ICO (Windows Icon) *.ICO&#8221; from the list of available file formats. The size of the final image should either be 16&#215;16 or 32&#215;32 pixels, larger images would just need to be scaled down by the browser.</p>
<h2>Online tools</h2>
<p>Following is a list of tools on the web that make it even easier to create favicons. In principle, you can upload an image file that you already have to the service, which will create a favicon file from that image. </p>
<ul>
<li><a href="http://favicon-generator.org/">Favicon Generator</a></li>
<li><a href="http://favikon.com/">Favikon</a></li>
<li><a href="http://www.favicon.cc/">favicon.ico Generator</a></li>
<li><a href="http://www.chami.com/html-kit/services/favicon/">FavIcon from Pics</a></li>
</ul>
<h2>On the fly</h2>
<p>Using the <a href="http://en.wikipedia.org/wiki/Data:_URI_scheme">data URI scheme</a>, it is possible to include data items, such as an image, inline on a webpage, without actually referencing it as an external resource. The format for that URI scheme is:</p>
<pre><code>data:[&lt;MIME-type&gt;][;base64],&lt;data&gt; </code></pre>
<p>The <a href="http://validator.w3.org/">W3C Validator</a> for example makes use of this. If your markup validation fails, the red square displayed in the address bar is referenced as:</p>
<pre><code>&lt;link rel="icon" href="data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%02%00%00%00%90%91h6%00%00%00%19IDAT(%91c%BCd%AB%C2%40%0A%60%22I%F5%A8%86Q%0DCJ%03%00%DE%B5%01S%07%88%8FG%00%00%00%00IEND%AEB%60%82" type="image/png" /&gt;</code></pre>
<p>In the example from the W3C Validator, the data for the favicon is represented using <a href="http://en.wikipedia.org/wiki/ASCII">ASCII</a> encoding, but could also be encoded as <a href="http://en.wikipedia.org/wiki/Base64">base64</a>, which would then look like this in the HTML source:</p>
<pre><code>&lt;link rel="icon" href="data:image/png;base64,/9j/4AAQ..." type="image/png" /&gt;</code></pre>
<h2>Referencing it in HTML</h2>
<p>When you have your favicon file, place it somewhere on your webspace of your site (it does not have to be the root directory) and then reference the file in HTML (in the head of the document) like so:</p>
<pre><code>&lt;link rel="Shortcut Icon"
      href=""http://example.com/favicon.ico"
      type="image/x-icon"&gt;</code></pre>
<p>If you now go to your site and refresh the browser, you should see your icon showing up in the address bar.</p>
<p>On a sidenote: if you ever wanted to play <a href="http://en.wikipedia.org/wiki/Defender_(video_game)">Defender</a> displayed in a 16&#215;16 pixels favicon, <a href="http://www.p01.org/releases/DHTML_contests/files/DEFENDER_of_the_favicon/">you can do so now</a>. Not necessarily useful for everyday websites, but a great demo of what can be done with favicons.</p>
<h2>Resources</h2>
<ul>
<li><a href="http://www.w3.org/2005/10/howto-favicon">How to Add a Favicon to your Site &#8211; QA @ W3C</a></li>
<li><a href="http://www.photoshopsupport.com/tutorials/jennifer/favicon.html">Mysteries Of The Favicon.ico — How To Create A Favicon In Photoshop</a></li>
<li><a href="http://www.amenco.com/golivein24//tips/favicon/">Creating favicons with Adobe Photoshop and GoLive</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2008/10/07/faviconsdemystified/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing Apache, MySQL and PHP on Leopard</title>
		<link>http://www.klauskomenda.com/archives/2008/10/07/installing-apache-mysql-and-php-on-leopard/</link>
		<comments>http://www.klauskomenda.com/archives/2008/10/07/installing-apache-mysql-and-php-on-leopard/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 18:11:21 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=611</guid>
		<description><![CDATA[Having a proper Web Development environment set up on your development machine is essential to test and, well, develop. As I had to go through that process again recently, I thought that documenting it for future reference might be a good idea.]]></description>
			<content:encoded><![CDATA[<p>Working in Web Development means you have to have a development environment installed on your local machine, in order to test and develop dynamic pages, using a web server (<a href="http://httpd.apache.org/">Apache,</a>), a database (<a href="http://www.mysql.com/">MySQL</a>) and a scripting language (<a href="http://www.php.net/">PHP</a>). There are ways of getting those components installed in a bundle, like <a href="http://en.wikipedia.org/wiki/WAMP">WAMP</a>, <a href="http://en.wikipedia.org/wiki/LAMP_(software_bundle)">LAMP</a> or <a href="http://en.wikipedia.org/wiki/MAMP">MAMP</a>. But as a developer, you are more the manual type, right? So as I had to go through that installation process recently, this article documents the steps I went through. </p>
<h2>Apache</h2>
<p><a href="http://www.apple.com/macosx/">OSX</a> already comes with Apache installed, it is just a matter of starting the server. You can do this if you go to System Preferences &gt; Sharing and check &#8220;Web Sharing&#8221;. The Apache default page should now be displayed at </p>
<pre><code>http://localhost</code></pre>
<p>Later on, you can use the following commands to start, stop or restart Apache:</p>
<p><kbd>$ sudo apachectl start<br />
$ sudo apachectl stop<br />
$ sudo apachectl restart<br />
</kbd></p>
<p>If you would like to change the DocumentRoot of the server, you need to edit the httpd.conf file:</p>
<p><kbd>$ sudo vi /etc/apache2/httpd.conf</kbd></p>
<p>In here, you need to change the DocumentRoot setting:</p>
<pre><code>DocumentRoot "/Users/myUser/myNewWebroot/"
&lt;Directory "/Users/myUser/myNewWebroot/"&gt;
...
&lt;/Directory&gt;</code></pre>
<h2>PHP</h2>
<p>PHP comes bundled up with Leopard as well. The important things to know here are where it got installed and where to find the configuration file. </p>
<p>Most likely, it got installed to:</p>
<div class="code">
/usr/local/php5
</div>
<p>The configuration file should be located at:</p>
<div class="code">
/private/etc/php.ini
</div>
<p>You only need to make sure that Apache knows that PHP is available, so edit httpd.conf:<br />
<kbd>$ sudo vi /etc/apache2/httpd.conf</kbd></p>
<p>And add the following lines (in the appropriate sections, to keep things tidy):</p>
<div class="code">
AddType application/x-httpd-php .php<br />
AddType application/x-httpd-php-source .phps<br />
&#8230;<br />
LoadModule php5_module        libexec/apache2/libphp5.so
</div>
<p>Finished with that, restart Apache, empty the browser cache and then load a php file for testing if it is correctly interpreted. </p>
<h2>MySQL</h2>
<p>Download the <a href="http://dev.mysql.com/downloads/mysql/5.0.html#macosx-dmg">most recent dmg image</a> from the MySQL site.</p>
<p>Before actually installing MySQL, I found it helps to restart the computer before proceeding with the installation. When running through the installation wizard, MySQL will get installed to:</p>
<div class="code">
/usr/local/mysql-VERSION
</div>
<p>So, for example:</p>
<div class="code">
/usr/local/mysql-5.0.51b-osx10.5-x86/
</div>
<p>Also, a symlink should have been created:</p>
<div class="code">
/usr/local/mysql -> mysql-5.0.51b-osx10.5-x86
</div>
<p>You should also install the Preference Pane, which comes with the installation package as <span class="code">MySQL.prefPane</span></p>
<p>To start MySQL manually, run the following command:<br />
<kbd>$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM start</kbd></p>
<p>You should also add MySQL to $PATH:<br />
<kbd>$ vi ~/.profile<br />
$ export PATH=$PATH:/usr/local/mysql/bin<br />
$ source ~/.profile<br />
</kbd></p>
<p>To check whether that was successful, run:<br />
<kbd>$ echo $PATH</kbd></p>
<p>The default settings for the root user are:</p>
<ul>
<li>Username: root</li>
<li>Password: [leave blank]</li>
</ul>
<h2>Add-on: PHPmyAdmin</h2>
<p>To get <a href="http://www.phpmyadmin.net/">PHPmyAdmin</a> installed, which comes in handy for managing your database(s), download the latest package from <a href="http://www.phpmyadmin.net/home_page/downloads.php">their download page</a>. Extract that package to a directory somewhere in your DocumentRoot.</p>
<p>Open <span class="code">config.sample.inc.php</span> with an editor of your choice and add the following details for your MySQL installation:</p>
<pre><code>/*
 * This is needed for cookie based authentication to encrypt password in
 * cookie
 */
$cfg['blowfish_secret'] = 'whatever'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

$cfg['Servers'][$i]['user']          = 'root';
$cfg['Servers'][$i]['password']      = ''; // use here your password</code></pre>
<p>After you made those changes, save the file as <span class="code">config.inc.php</span>.</p>
<h2>PEAR</h2>
<p><a href="http://pear.php.net/">PEAR</a> should also already be available on your Mac. The location is probably:</p>
<div class="code">
/usr/local/php5/bin/pear
</div>
<p>It is a good idea to add the path to PEAR to $PATH, similar to setting the path for MySQL (see above). In addition, upgrade PEAR to the latest version like so:</p>
<p><kbd>$ sudo pear channel-update pear.php.net<br />
$ sudo pear upgrade PEAR<br />
</kbd></p>
<h2>Resources</h2>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/mac-os-x-installation.html">Installing MySQL on Mac OS X (MySQL Reference Manual)</a></li>
<li><a href="http://unlettered.org/2007/11/14/installing-mysql-50-on-mac-os-x-105-client/">Installing MySQL 5.0 on Max OS X 10.5 client</a></li>
<li><a href="http://switch.richard5.net/isp-in-a-box-v2/installing-mysql-on-mac-os-x/">Installing MySQL on Mac OS X</a></li>
</li>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2008/10/07/installing-apache-mysql-and-php-on-leopard/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Module Pattern vs. Custom Objects</title>
		<link>http://www.klauskomenda.com/archives/2008/10/01/module-pattern-vs-custom-objects/</link>
		<comments>http://www.klauskomenda.com/archives/2008/10/01/module-pattern-vs-custom-objects/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 09:58:54 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=505</guid>
		<description><![CDATA[I have written about different JavaScript Programming Patterns before, but this time it is more about answering the question: Can you achieve the same thing with the Module Pattern as with Custom Objects? News at 11.]]></description>
			<content:encoded><![CDATA[<p><a href="/code/javascript-programming-patterns/">A little while ago</a> I tried to gather together information about what kind of programming patterns are out there in the wild when it comes to JavaScript and describing the characteristics of each of those. At <a href="http://uk.yahoo.com">Yahoo!</a>, the two patterns we use almost exclusively are <a href="/code/javascript-programming-patterns/#module">Module Pattern</a> (for functionality to be implemented on various properties) and <a href="/code/javascript-programming-patterns/#custom">Custom Objects</a> (as in the <a href="http://developer.yahoo.com/yui/">YUI library</a>, for example). The question I was asking myself is how they behave differently and whether you could do the same thing with both of them.</p>
<p>For this article I am sticking to the <a href="/wp-content/uploads/2007/08/module.html">same simple example</a> that I already used in my previous article. But to make it a little bit more interesting, we have two lists of links, not just one:</p>
<pre><code >&lt;ul id="list-1"&gt;
    &lt;li&gt;&lt;a href="http://news.bbc.co.uk/"&gt;News on BBC website&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://nytimes.com/"&gt;Frontpage of The New York Times&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.guardian.co.uk/"&gt;Guardian Unlimited&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ul id="list-2"&gt;
    &lt;li&gt;&lt;a href="http://valleywag.com/"&gt;Valleywag&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://techcrunch.com/"&gt;TechCrunch&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.yahoo.com/"&gt;Yahoo!&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</code></pre>
<p>Clicking on one of the links should simply switch the background color to a value we would like to provide as a parameter. Essentially, we want to have two separate objects in the end that act independently from each other. </p>
<h2>Custom Objects</h2>
<p>The structure of the source code roughly remains the same as the one used in my previous article. The only difference is that I set up the constructor function in a way that it is possible to pass values to it (colors, Id of the list and some random value which we will use later to prove that we have two distinct objects).</p>
<pre><code>function anchorChangerProto (colors, list, random) {
    this.config = {
        colors: colors,
        list: list
    }

    this.test = random;
}</code></pre>
<p>No rocket science, pretty straight forward. I am also creating a <span class="code">logRandom()</span> function which will log the value of test to the console.</p>
<pre><code>anchorChangerProto.prototype.logRandom = function () {
    console.log(this.test);
}</code></pre>
<p>Nothing unexpected in here either. So the full source code for anchorChangerProto looks like this:</p>
<pre><code>function anchorChangerProto (colors, list, random) {
    this.config = {
        colors: colors,
        list: list
    }

    this.test = random;
}

anchorChangerProto.prototype.changeColor = function (linkObj, newColor) {
    linkObj.style.backgroundColor = newColor;
};

anchorChangerProto.prototype.init = function () {
    var self = this;

    // get all links from particular list
    var anchors = this.config.list.getElementsByTagName("a");
    var size = anchors.length;

    for (var i = 0; i &lt; size; i++) {
        anchors[i].color = self.config.colors[i];

        anchors[i].onclick = function () {
            self.changeColor(this, this.color);
            return false;
        };
    }
};

anchorChangerProto.prototype.logRandom = function () {
    console.log(this.test);
}</code></pre>
<p>At the end of the page, in a &lt;script&gt; block I can now instantiate my objects and call their init methods.</p>
<pre><code>var changer1 = new anchorChangerProto([ "#F63", "#CC0", "#CFF" ], document.getElementById("list-1"), "first changer");
changer1.init();

var changer2 = new anchorChangerProto([ "#009", "#990", "#909" ], document.getElementById("list-2"), "second changer");
changer2.init();</code></pre>
<p>The new operator creates a generic object, sets its __proto__ value to anchorChangerProto.prototype, and passes that object to the constructor function as the this keyword. I tried to explain that in my other article about <a href="/code/javascript-inheritance-by-example/">inheritance in JavaScript</a>. </p>
<p>After these statements, we can call the logRandom function for each object to prove that we have indeed created two distinct objects which act independently from each other. Have a look at the <a href='http://www.klauskomenda.com/wp-content/uploads/2008/10/custom_objects2.html'>demo page for this example</a>.</p>
<pre><code>changer1.logRandom(); // logs: "first changer"
changer2.logRandom(); // logs: "second changer"</code></pre>
<p>Everything as expected. Now the question is whether the same thing can be achieved with the Module Pattern.</p>
<h2>Module Pattern</h2>
<p>It can be achieved, with some slight modifications. The main characteristics of the Module Pattern are that an anonymous function gets executed immediately and returns an object holding the publicly available properties for that object. In addition to that, you can also have private properties and methods, which can not be accessed from outside. So the basic structure is:</p>
<pre><code>var myModule = function () {
    ... some private properties and methods ...
    return {
        ... some public properties and methods ...
    }
}();</code></pre>
<p>The parens at the end ensure that the function gets called immediately, the object gets returned and assigned to the variable myModule. Now the first step to achieve the same thing as above is to remove those parens. </p>
<pre><code>var myModule = function () {
    ... some private properties and methods ...
    return {
        ... some public properties and methods ...
    }
};</code></pre>
<p>The reason for removing them is the following: If we keep them in there, myModule is always going to be the only one module of that kind on the page. The function will be executed, the object returned and assigned to the variable. You would not be able to create a distinctively different second object of that kind on the page.</p>
<p>However, by leaving out the parens at the end, we can simply call that function multiple times and always get a new object back which is unique. But not so fast. Before that I would like to show you the source code using the (slightly modified) Module Pattern: </p>
<pre><code>var anchorChangerModule = function () {

    // private property
    var config = {};

    // this is a private method
    // can be accessed within anchorChange3
    // cannot be accessed from outside
    function alterColor(linkObj, color) {
        linkObj.style.backgroundColor = color;
    }

    return {
        // public method
        // can be accessed from outside
        changeColor: function (linkObj, newColor) {
            // calls private function to change color
            alterColor(linkObj, newColor);
        },

        // public method
        // can be accessed from outside
        init: function (colors, list, random) {
            var self = this; // assign reference to current object to "self"

            config.colors = colors;
            this.test = random;

            // get all links on the page
            var anchors = list.getElementsByTagName("a");
            var size = anchors.length;

            for (var i = 0; i &lt; size; i++) {
                anchors[i].color = config.colors[i];

                anchors[i].onclick = function () {
                    self.changeColor(this, this.color); // this is bound to the anchor object
                    return false;
                };
            }
        },

        logRandom: function () {
            console.log(this.test);
        }
    };
};</code></pre>
<p>Again, nothing crazy going on here. As with the Custom Objects example above, I made sure that it is possible to pass in values to the init function and assign them to properties of the object and added the logRandom function. Now I can create the objects and the end of the page in a &lt;script&gt; block:</p>
<pre><code>var changer1 = anchorChangerModule();
changer1.init([ "#F63", "#CC0", "#CFF" ], document.getElementById("list-1"), 'first changer');

var changer2 = anchorChangerModule();
changer2.init([ "#009", "#990", "#909" ], document.getElementById("list-2"), 'second changer');</code></pre>
<p>What I am doing here, as opposed to above when using the new operator, is simply calling the anchorChangerModule function, which returns an object, which then gets assigned to changer1 and changer2, respectively.  To prove again that we have indeed two distinct objects, let us call the logRandom function:</p>
<pre><code>changer1.logRandom(); // logs "first changer"
changer2.logRandom(); // logs "second changer"</code></pre>
<p>Again, we can <a href='http://www.klauskomenda.com/wp-content/uploads/2008/10/module2.html'>clearly see</a> that we have two different objects who operate independently from each other. And now, in fact, we have achieved the same thing as we did before with Custom Objects using the Module Pattern. The same thing? Well, not quite&#8230;</p>
<h2>A Word about Inheritance</h2>
<p>Taking a look at this simple example, yes, we have achieved the same functionality with both programming patterns. But the drawback is that if you strictly stick to the Module Pattern, no inheritance chain is being set up. You would not be able to create a sub-module of anchorChangerModule and make it inherit functionality from the parent. For this to work, you would need to use .prototype so that when calling a method, JavaScript will work its way up the inheritance chain using the __proto__ property of the object. As far as I know, this can only be done using .prototype and when instantiating a new instance of an object using the new operator. </p>
<p>But then again, I believe that the Module Pattern was not designed for setting up inheritance or replacing Custom Objects.</p>
<h2>Feedback appreciated</h2>
<p>I would not consider myself a JavaScript expert, by no means, therefor the conclusions I came up with which are stated above are just based on my own experiments and research. I would highly appreciate it if you guys could point out mistakes or flaws that I made in my conclusions and explanations, if you spot any. Thanks. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2008/10/01/module-pattern-vs-custom-objects/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>INSERT command denied to user? Clean up your DB!</title>
		<link>http://www.klauskomenda.com/archives/2008/09/18/insert-command-denied-to-user-clean-up-your-db/</link>
		<comments>http://www.klauskomenda.com/archives/2008/09/18/insert-command-denied-to-user-clean-up-your-db/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 21:50:09 +0000</pubDate>
		<dc:creator>Klaus</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.klauskomenda.com/?p=376</guid>
		<description><![CDATA[For those of you trying to post comments recently: Apologies. I first blamed WordPress for it, but it turned out that it was rather a bad (admin) job on my end.]]></description>
			<content:encoded><![CDATA[<p>After posting my <a href="/archives/2008/09/18/whole-lot-of-leavin/">most recent blogpost</a>, I noticed that I was neither able to create a new one or to post comments on the site. <a href="http://wordpress.org">WordPress</a> also supplied me recently with some strange comment notification Emails saying &#8216;[klauskomenda.com] Please moderate: &#8220;&#8221;&#8216;. But the notification Email, that normally contains the message someone left on the site was pretty empty. That looked suspicious and I suspected some sort of hacker attack. </p>
<p>When checking the response coming back from the MySQL database when WordPress tried to insert rows, I got something like this:</p>
<pre><code>1142 - INSERT command denied to user 'abc'@'host.com' for table 'comments'</code></pre>
<p>That made me think: &#8220;Hm, did my hosting provider remove my DB rights because I paid the bill a few days later than usual?&#8221;. Nah, that can&#8217;t be. Checking on the hosters website confirmed that, in fact, everything was settled regarding invoices. </p>
<p>And then it hit me. On the DB tab in the hosting providers admin interface it said: &#8220;Size: 10 MB of 10 MB used.&#8221; D&#8217;oh. So that was the reason for that error and, consequently, why no &#8216;inserts&#8217; couldn&#8217;t be performed anymore. </p>
<p>So it was not some bad hacker that caused me headaches.. Turns out, I did not show enough TLC recently to the MySQL database that powers this blog and let it grow and grow over the last months, even years, without deleting tables that were not needed anymore. After doing that, doing some cleanup, I got it down to 5 MB again. And we&#8217;re back in business <img src='http://www.klauskomenda.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><em>Attention</em>: If you delete a reasonable amount of data from the database and clear up some space, it might still take up to 30 minutes (at least with my hosting provider), until you are able to run INSERT commands again. So don&#8217;t panic if that happens, just be a little patient, grab a coffee, come back and should be fine.</p>
<p>Again, apologies to everyone who wanted to leave a comment. It got lost in the wide open spaces of the internets.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klauskomenda.com/archives/2008/09/18/insert-command-denied-to-user-clean-up-your-db/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

