<?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>web(cslai) &#187; Javascript</title>
	<atom:link href="http://cslai.coolsilon.com/category/web-development/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://cslai.coolsilon.com</link>
	<description>Findings and Notes in Web Development</description>
	<lastBuildDate>Tue, 06 Sep 2011 08:15:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Writing jQuery Plugin 101</title>
		<link>http://cslai.coolsilon.com/2010/03/04/writing-jquery-plugin-101/</link>
		<comments>http://cslai.coolsilon.com/2010/03/04/writing-jquery-plugin-101/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 17:25:05 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=145</guid>
		<description><![CDATA[I often struggle to get my Javascript code organized, and have tried numerous ways to do so. I have tried putting relevant code into classes and instantiate as needed, then abuse jQuery&#8217;s data() method to store everything (from scalar values to functions and callbacks). Recently, after knowing (briefly) how a jQuery plugin should be written, [...]]]></description>
			<content:encoded><![CDATA[<p>I often struggle to get my Javascript code organized, and have tried numerous ways to do so. I have tried putting relevant code into <a href="http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/">classes</a> and instantiate as needed, then abuse jQuery&#8217;s <code class="javascript">data()</code> method to store everything (from scalar values to functions and callbacks). Recently, after knowing (briefly) how a jQuery plugin should be written, it does greatly simplify my code.</p>
<p><span id="more-145"></span></p>
<p>While there are still a lot to learn about plugin authoring, I would only jot down what I know right now. As the code may not conform to the best practice, please let me know if there is a better way.</p>
<p>To begin, we start with an empty html page</p>
<p><code class="html"><br />
&lt;html&gt;<br />
&lt;head&gt;<br />
       &lt;title&gt;Title&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
       &lt;input type="text" id="a" /&gt;&lt;br /&gt;<br />
       &lt;input type="text" id="b" /&gt;&lt;br /&gt;<br />
       &lt;input type="text" id="c" /&gt;<br />
       &lt;script type="text/javascript" src="./jquery-1.4.1.min.js"&gt;&lt;/script&gt;<br />
       &lt;script type="text/javascript" src="./script.js"&gt;&lt;/script&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>The first included Javascript script is obviously jQuery, the second file (script.js) is my code, which is not named properly. The basic structure of the file should look like follows:</p>
<p><code class="javascript"><br />
(function($) {<br />
    var module_init = function(plugin, module) {<br />
            return function(action) {<br />
                var arg = trim_argument(arguments);<br />
                return this.each(function() {<br />
                    if($.fn[plugin][module][action]) {<br />
                        $.fn[plugin][module][action]<br />
                            .apply(this, arg);<br />
                    } else {<br />
                        throw(action + '() does not exist');<br />
                    }<br />
                })<br />
            }<br />
        },<br />
&nbsp;<br />
        module_exists = function(plugin, module) {<br />
            return $.fn[plugin][module];<br />
        },<br />
&nbsp;<br />
        shift_argument = function(item, arg) {<br />
            result = [item];<br />
&nbsp;<br />
            for(i = 0; i < arg.length; i++) {<br />
                result.push(arg[i]);<br />
            }<br />
&nbsp;<br />
            return result;<br />
        },<br />
&nbsp;<br />
        trim_argument = function(arg) {<br />
            result = [];<br />
&nbsp;<br />
            if(arg.length > 1) {<br />
                for(i = 1; i < arg.length; i++) {<br />
                    result.push(arg[i]);<br />
                }<br />
            }<br />
&nbsp;<br />
            return result;<br />
        };<br />
&nbsp;<br />
        // further code below this line<br />
})(jQuery);<br />
</code></p>
<p>Let us just ignore the functions above for now and proceed to the actual plugin code first. What we're trying to achieve here is to post whatever written to #a is posted to #b, and #b post it to #c. The function call should be as follows:</p>
<p><code class="javascript"><br />
$(selector).foo(sub_module, options);<br />
// or<br />
$(selector).foo(sub_module, action, [parameter_if_any]);<br />
</code></p>
<p><code class="javscript">foo</code> is the name of the example plugin, and <code class="javascript">sub_module</code> determines which sub-module is responsible to carry out the actual action specified by <code class="javascript">action</code>. If <code class="javascript">options</code> is passed in instead, then it would trigger <code class="javascript">init()</code> for the respective sub-module.</p>
<p>In short, <code class="javascript">foo()</code> is there just to notify the sub-module to call the right action, hence the code should be simple, as follows (add after the last comment in the basic structure code shown above):</p>
<p><code class="javascript"><br />
    (function foo() {<br />
        $.fn.foo = function(module, param) {<br />
            var arg = trim_argument(arguments);<br />
&nbsp;<br />
            return this.each(function() {<br />
                if(module_exists('foo', module)) {<br />
                    if(typeof param != 'string') {<br />
                        $.fn.foo[module]<br />
                            .apply($(this), shift_argument('init', arg));<br />
                    } else {<br />
                        $.fn.foo[module]<br />
                            .apply($(this), arg);<br />
                    }<br />
                } else {<br />
                    throw(module + ' module does not exists');<br />
                }<br />
            });<br />
        }<br />
    })();<br />
</code></p>
<p>I have a habit to group all the relevant bits together, hence the code is enclosed in an immediately executed function block. It does what it is designed to do -- to delegate the function call to the respective sub-module. <code class="javascript">trim_argument();</code> is there to remove the first argument (in this case <code class="javascript">module</code>), and <code class="javascript">shift_argument();</code> is used to insert an argument into the existing argument list. These two function functions are written to enable us to manipulate the arguments to be used in <code class="javascript">Function.apply()</code> method. Of course, if the specified sub-module does not exist, the code will throw an exception.</p>
<p>Now that we have the main plugin code written, next is to start developing the submodules. Firstly, is a plugin for the text box #a. The textbox is supposed to show textbox #b and passes the input text to #b whenever the value is not null. The code is to be added right after the main plugin, as mentioned above.</p>
<p><code class="javascript"><br />
    (function alpha() {<br />
        $.fn.foo.alpha = module_init('foo', 'alpha');<br />
&nbsp;<br />
        var alpha = $.fn.foo.alpha;<br />
&nbsp;<br />
        alpha.init = function(options) {<br />
            $(this)<br />
                .data('options', $.extend({}, options))<br />
                .keyup(alpha.alpha_update);<br />
        };<br />
&nbsp;<br />
        alpha.alpha_update = function() {<br />
            $(this)<br />
                .data('options').b<br />
                    .foo('bravo', 'alpha_update', $(this).val());<br />
        };<br />
    })();<br />
</code></p>
<p><code class="javascript">module_init</code> is there to do the actual delegation of action. Again, it will check with the remaining arguments after removing the first, which is the <code class="javascript">action</code>. The function will then attempt to call the action if available, otherwise a new exception is thrown.</p>
<p><code class="javascript"><abbr title="$.fn.foo.alpha">alpha</abbr>.init</code> is a compulsory method that initializes the sub-module. On the other hand, <code class="javascript">alpha.alpha_update</code> is the actual code that updates another textbox via another sub-module, which is named 'bravo', and coded as follows:</p>
<p><code class="javascript"><br />
    (function bravo() {<br />
        $.fn.foo.bravo = module_init('foo', 'bravo');<br />
&nbsp;<br />
        var bravo = $.fn.foo.bravo;<br />
&nbsp;<br />
        bravo.init = function(options) {<br />
            $(this)<br />
                .data('options', $.extend({}, options))<br />
                .hide();<br />
        };<br />
&nbsp;<br />
        bravo.hide = function() {<br />
            $(this).fadeOut('fast');<br />
        };<br />
&nbsp;<br />
        bravo.show = function() {<br />
            $(this).fadeIn('fast');<br />
        };<br />
&nbsp;<br />
        bravo.toggle = function(value) {<br />
            if($(this).is(':hidden') &#038;&#038; value.length > 0) {<br />
                bravo.show.call(this);<br />
            } else if($(this).is(':visible') &#038;&#038; value.length == 0) {<br />
                bravo.hide.call(this);<br />
            }<br />
        };<br />
&nbsp;<br />
        bravo.alpha_update = function(value) {<br />
            $(this)<br />
                .val('bravo: ' + value)<br />
                .data('options').c<br />
                    .foo('charlie', 'alpha_update', value);<br />
&nbsp;<br />
            bravo.toggle.call(this, value);<br />
        };<br />
    })();<br />
</code></p>
<p>The bravo code basically does what it is intended to, which is to receive update from alpha, and then toggle the display of the text box depending on the length of passed in value. After that, bravo is going to post the received update to charlie, as follows:</p>
<p><code class="javascript"><br />
    (function charlie() {<br />
        $.fn.foo.charlie = module_init('foo', 'charlie');<br />
&nbsp;<br />
        var charlie = $.fn.foo.charlie;<br />
&nbsp;<br />
        charlie.init = function(options) {<br />
            $(this)<br />
                .data('options', $.extend({}, options))<br />
                .hide();<br />
        };<br />
&nbsp;<br />
        charlie.hide = function() {<br />
            $(this).fadeOut('slow');<br />
        };<br />
&nbsp;<br />
        charlie.show = function() {<br />
            $(this).fadeIn('slow');<br />
        };<br />
&nbsp;<br />
        charlie.toggle = function(value) {<br />
            if($(this).is(':hidden') &#038;&#038; value.length > 0) {<br />
                charlie.show.call(this);<br />
            } else if($(this).is(':visible') &#038;&#038; value.length == 0) {<br />
                charlie.hide.call(this);<br />
            }<br />
        };<br />
&nbsp;<br />
        charlie.alpha_update = function(value) {<br />
            $(this).val('charlie: ' + value);<br />
&nbsp;<br />
            charlie.toggle.call(this, value);<br />
        };<br />
</code></p>
<p>After receiving data from bravo, likewise, it updates the textbox, then display or hide the textbook accordingly. For every action method call, <code class="javascript">this</code> keyword is usually similar to the jQuery callback functions, which refers to the respective html element (not wrapped with jQuery functions), hence the function call within the sub-module is often via <code class="javascript">Function.call() /* or */ Function.apply()</code>.</p>
<p>To run the code, one only need the following code:</p>
<p><code class="javascript"><br />
    $(function() {<br />
        var a = $('#a'),<br />
            b = $('#b'),<br />
            c = $('#c');<br />
&nbsp;<br />
        a.foo('alpha', { b: b, c: c }).focus();<br />
        b.foo('bravo', { a: a, c: c });<br />
        c.foo('charlie', { a: a, b: b });<br />
    });<br />
</code></p>
<p>That's all, we have done a very simple, and pointless plugin.</p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2010/03/04/writing-jquery-plugin-101/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My Collection of Common Javascript Functions &#8211; Part 1</title>
		<link>http://cslai.coolsilon.com/2009/06/04/my-collection-of-common-javascript-functions-part-1/</link>
		<comments>http://cslai.coolsilon.com/2009/06/04/my-collection-of-common-javascript-functions-part-1/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 13:19:14 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=121</guid>
		<description><![CDATA[After coded enough Javascript few months back, I found that there are a couple of functions that I kept re-using in different projects. Therefore I took some time to refactor them and re-arrange them into a single file. The common code that I keep reusing even today consists of functions that does prototypical inheritance, scope [...]]]></description>
			<content:encoded><![CDATA[<p>After coded enough Javascript few months back, I found that there are a couple of functions that I kept re-using in different projects. Therefore I took some time to refactor them and re-arrange them into a single file. The common code that I keep reusing even today consists of functions that does prototypical inheritance, <a href="http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/">scope maintenance</a>, some <a href="http://www.jquery.com/">jquery</a> stuff, <a href="http://code.google.com/apis/maps/">google maps api</a> stuff and some general ajax application related code.</p>
<p><span id="more-121"></span></p>
<h3><del datetime="2010-02-18T06:44:55+00:00">Prototypical</del>Prototypal Inheritance</h3>
<p>Basically after reading through some articles across the internet, especially <a href="http://javascript.crockford.com/prototypal.html">this article</a>, then I have this piece of code in my collection.</p>
<pre class="ln-"><code class="javascript">if(typeof Object.create !== 'function') {
    Object.create = function(_o) {
        var F = function () {}
        F.prototype = _o;
        return new F();
    };
}</code></pre>
<p>It is basically the same piece of code because I find it cleaner and more efficient than my initial draft.</p>
<h3>Namespacing</h3>
<p>Everyone agrees that global variables are evil, therefore I group all my common functions and classes into one single object, as follows,</p>
<pre class="ln-"><code class="javascript">var cs = Object.create(new function() {
    // some other code
}</code></pre>
<p>And I find the way <a href="http://developer.yahoo.com/yui">YUI</a> manages the modules good enough for me, then I emulated YAHOO.namespace function by having this in my object.</p>
<pre class="ln-"><code class="javascript">    this.namespace = function(_namespace) {
        if(!this[_namespace]) {
            this[_namespace] = Object.create(new function() {});
        }

        return this[_namespace];
    }</code></pre>
<p>The code may not be clever/clean, but as long as it does the job I have no complaint.</p>
<h3>Preserving State</h3>
<p>I didn&#8217;t quite like the delegate function as I can&#8217;t pass in extra arguments, therefore I have this after some refactoring</p>
<pre class="ln-"><code class="javascript">    var __delegate = function(_method, _scope) {
        var _arguments = arguments;
        return function() {
            return _method.apply(
                _scope, compile_arguments.call(
                    this, _arguments, [], 2
            ));
        }
    };

    var compile_arguments = function(_arguments, _result, _start) {
        for(var i = _start; _arguments.length > _start &#038;&#038; i < _arguments.length; i++) {
            _result.push(_arguments[i]);
        }

        return _result;
    };

    this.delegate = __delegate;
</code></code></pre>
<h3>Emulate Functional Programming</h3>
<p>I have recently decided to learn a little bit on functional programming recently but I haven&#8217;t decide which language I am picking. I have some experience in SWI-Prolog but prolog is just a declarative functional programming and does not seem to offer closure, higher order functions and currying. Anyway, back to the topic, after spending some time in this <a href="http://eloquentjavascript.net/chapter6.html">book</a>, I modified some code in the book and result in&#8230;</p>
<pre class="ln-"><code class="javascript">    this.functional = Object.create(new function() {
        this.map = function(_function, _arguments) {
            var result = [];

            for(var i = 0; i < _arguments.length; i++) {
                result.push(_function(_arguments[i]));
            }

            return result;
        };

        this.reduce = function(_combine, _base, _arguments) {
            return _arguments.length > 0 ?
                this.reduce(
                    _combine, _combine(_base, _arguments.shift()),
                    _arguments
                ) : _base;
        };
    });</code></pre>
<p>I am not sure if I can do the similar recursion in cs.functional.map compared to cs.functional.reduce because I am rather weak in recursion.</p>
<p><del datetime="2009-08-15T14:20:38+00:00">That&#8217;s all for now, and in the second part of this article I will post some helper functions for use with some third party libraries like jQuery and Google Maps API.</del></p>
<p>EDIT 15/8/2009 There are some changes to this set of functions, will post an update soon and there will probably no part 2 any time soon.</p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/06/04/my-collection-of-common-javascript-functions-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maintaining State with jQuery Event</title>
		<link>http://cslai.coolsilon.com/2009/05/04/maintaining-state-with-jquery-event/</link>
		<comments>http://cslai.coolsilon.com/2009/05/04/maintaining-state-with-jquery-event/#comments</comments>
		<pubDate>Mon, 04 May 2009 13:17:24 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=118</guid>
		<description><![CDATA[Maintaining state in Javascript is not too difficult once you catch the idea. However, as I am not a super brilliant programmer, it takes me some time to find a way to maintain state as YUI Event does in jQuery. I like grouping relevant functions into a class as it makes code easier to manage [...]]]></description>
			<content:encoded><![CDATA[<p>Maintaining state in Javascript is not too difficult once you catch the idea. However, as I am not a super brilliant programmer, it takes me some time to find a way to maintain state as <a href="http://developer.yahoo.com/yui/event/">YUI Event</a> does in <a href="http://jquery.com/">jQuery</a>.</p>
<p><span id="more-118"></span></p>
<p>I like grouping relevant functions into a class as it makes code easier to manage (for me, as there may be better ways. As I just recently found out how prototyping inheritance works, it is possible that the discovery may change the way I code in future), therefore the scope of event handler functions is very important. Making the scope (value/reference of <code class="javascript">this</code>) to the DOM element listened to the event does not help in accessing the shared data so I needed a way to change that behavior.</p>
<p>To solve the problem, a small derivation of the <a href="http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/">previously discussed</a> delegate function will come in useful.</p>
<pre class="ln-">
<code class="javascript">
/**
 * Simulating YUI Event with jQuery
 */
(function($) {
EventHandler = function(_method, _scope) {
    var _argument = {};

    if(arguments.length > 2) {
        _argument = arguments[2];
    }

    // jquery default event callback format
    return function(_event) {
        // this = DOM element
        return _method.call(_scope, _event, this, _argument);
    };
};
})(jQuery);

var Foo = function() {

    var bar_handler = function(_event, _element) {
        alert('bar');
    }

    var foo_handler = function(_event, _element, _extra_argument) {
        alert(_extra_argument);
    }

    (function() {
        $('#foo').click(EventHandler(foo_handler, this, 'foo'));
        $('#bar').click(EventHandler(bar_handler, this));
    }).call(this);
};
var foo = new Foo();
</code>
</pre>
<p>That basically wraps up what I found these couple of days, if I manage to dig some time, the next post should be focused on prototypical inheritance.</p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/05/04/maintaining-state-with-jquery-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Proof of Concept: YQuery</title>
		<link>http://cslai.coolsilon.com/2009/05/04/proof-of-concept-yquery/</link>
		<comments>http://cslai.coolsilon.com/2009/05/04/proof-of-concept-yquery/#comments</comments>
		<pubDate>Mon, 04 May 2009 13:01:24 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=114</guid>
		<description><![CDATA[After the last post, I found that it may be fun to write a wrapper for YUI in order to make it behave like jQuery. Therefore, the code below is clearly mainly for self-amusement and is not intended to be used in production projects. However, through coding this, I found that although the difference in [...]]]></description>
			<content:encoded><![CDATA[<p>After the <a href="http://cslai.coolsilon.com/2009/04/11/maintaining-state-with-yui-event/">last post</a>, I found that it may be fun to write a wrapper for YUI in order to make it behave like <a href="http://jquery.com/">jQuery</a>. Therefore, the code below is clearly mainly for self-amusement and is not intended to be used in production projects. However, through coding this, I found that although the difference in design, but <a href="http://developer.yahoo.com/yui/">YUI</a> is obviously capable to do what jQuery offers (if not more). I will not continue working on this so whoever interested may just copy and paste the code to further developing it. </p>
<p><span id="more-114"></span></p>
<pre class="ln-">
<code class="javascript">/**
 * Proof of concept : jQuery-like syntax in YUI
 */
(function() {

var yui_builder = (function(Selector, Element, DOM, Event) {
    return function(_selector) {
        var Result = function() {};

        var init = function(_selector) {
            var result = [];

            if(typeof _selector == 'object') {
                result = [ new Element(_selector) ];
            } else {
                result = query_elements.call(this, _selector);
            }

            return result;
        };

        var delegate = function(_scope, _function) {
            return function() {
                return _function.apply(_scope, arguments);
            };
        };

        var query_elements = function(_selector) {
            var result = new Result;
            var elements = wrap_elements(Selector.query(_selector));

            for(var i in elements) {
                result[i] = elements[i];
            }

            return result;
        };

        var unwrap_elements = function(_elements) {
            var result = [];

            for(var i in _elements) {
                result[i] = _elements[i].get('element');
            }

            return result;
        };

        var wrap_elements = function(_elements) {
            var result = [];

            for(var i in _elements) {
                result[i] = new Element(_elements[i]);
            }

            return result;
        };

        return (function(_elements) {
            _this = new Result;

            _this.addClass = function(_class) {
                for(var i in _elements) {
                    _elements[i].addClass(_class);
                }

                return this;
            };

            _this.append = function(_content) {
                if(typeof _content == 'string') {
                    for(var i in _elements) {
                        _elements[i].set(
                            'innerHTML',
                            _elements[i].get('innerHTML') + _content
                        );
                    }
                } else if(typeof _content == 'object') {
                    _elements[0].appendChild(_content);
                }

                return this;
            };

            _this.appendTo = function(_selector) {
                var _parent = yui_builder(_selector);

                for(var i in _elements) {
                    _elements[i].appendTo(_parent.get(0));
                }

                return this;
            }

            _this.attr = function() {
                var result = this;

                if(arguments.length == 1 &#038;&#038; typeof arguments[0] == 'string') {
                    result = _elements[0].get(arguments[0]);

                    if(!result) {
                        result = undefined;
                    }
                } else if(arguments.length == 1 &#038;&#038; typeof arguments[0] == 'object') {
                    for(var i in _elements) {
                        for(var attribute in arguments[0]) {
                            _elements[i].set(attribute, arguments[0][attribute]);
                        }
                    }
                } else if(typeof arguments[1] == 'function') {
                    for(var i in _elements) {
                        _elements[i].set(arguments[0], arguments[1].call(_elements[i].get('element'), i));
                    }
                } else {
                    for(var i in _elements) {
                        _elements[i].set(arguments[0], arguments[1]);
                    }
                }

                return result;
            };

            _this.each = function(_callback) {
                for(var i in _elements) {
                    _callback.call(_elements[i].get('element'), i, _elements[i].get('element'));
                }

                return this;
            };

            _this.eq = function(_index) {
                return yui_builder(_elements[_index]);
            };

            _this.get = function() {
                var result = [];

                if(arguments.length == 0) {
                    result = unwrap_elements.call(this, _elements);
                } else {
                    result = _elements[arguments[0]].get('element');
                }

                return result;
            };

            _this.hasClass = function(_class) {
                var result = false;

                for(var i in _elements) {
                    if(_elements[i].hasClass(_class)) {
                        result = true;
                        break;
                    }
                }

                return result;
            };

            _this.html = function() {
                var result = this;

                if(arguments.length == 0) {
                    result = _elements[0].get('innerHTML');
                } else {
                    _elements[0].set('innerHTML', arguments[0]);
                }

                return result;
            }

            _this.index = function(_subject) {
                var result = -1;

                for(var i in _elements) {
                    if(_elements[i].get('element') === _subject) {
                        result = i;
                    }
                }

                return result;
            };

            _this.removeClass = function(_class) {
                for(var i in _elements) {
                    _elements[i].removeClass(_class);
                }

                return this;
            }

            _this.size = function() {
                return _elements.length;
            };

            _this.toggleClass = function() {
                if(arguments.length == 0) {
                    for(var i in _elements) {
                        if(_elements[i].hasClass) {
                            _elements[i].removeClass(arguments[0]);
                        } else {
                            _elements[i].addClass(arguments[0]);
                        }
                    }
                } else {
                    for(var i in _elements) {
                        if(arguments[1]) {
                            _elements[i].addClass(arguments[0]);
                        } else {
                            _elements[i].removeClass(arguments[0]);
                        }
                    }
                }

                return this;
            };

            _this.val = function() {
                var result = this;

                if(arguments.length == 0) {
                    result = this.attr('value');
                } else {
                    for(var i in _elements) {
                        this.attr('value', arguments[0]);
                    }
                }

                return result;
            };

            (function() {
                for(var i in _elements) {
                    _this[i] = _elements[i];
                }
            }).call(_this)

            return _this;
        }).call(this, init.apply(this, arguments));
    }
})(YAHOO.util.Selector, YAHOO.util.Element, YAHOO.util.Dom, YAHOO.util.Event);

})();
</code>
</pre>
<p>Most of the implemented functions are similar to what jQuery offers, therefore to use the yui_builder:</p>
<pre class="ln-">
<code class="javascript">
(function($) {
    // everyone loves $ function
    $(document.createElement('a'));
        .attr('href', 'test')
        .html('test')
        .appendTo(document.body);
})(yui_builder);
</code>
</pre>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/05/04/proof-of-concept-yquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maintaining State with YUI Event</title>
		<link>http://cslai.coolsilon.com/2009/04/11/maintaining-state-with-yui-event/</link>
		<comments>http://cslai.coolsilon.com/2009/04/11/maintaining-state-with-yui-event/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 04:39:30 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=107</guid>
		<description><![CDATA[When one start writting Javascript in patterns like the module pattern, then sooner or later he would want to maintain the state when an event handler is called. The reason I am still using YUI to handle my event handling code is because I like how state can be maintained. There are some other interesting [...]]]></description>
			<content:encoded><![CDATA[<p>When one start writting Javascript in patterns like the <a href="http://yuiblog.com/blog/2007/06/12/module-pattern/">module pattern</a>, then sooner or later he would want to maintain the state when an event handler is called. The reason I am still using YUI to handle my event handling code is because I like how state can be maintained.</p>
<p><span id="more-107"></span></p>
<p>There are some other interesting things in the <a href="http://developer.yahoo.com/yui/event/">Event utility</a> like <code class="javascript">YAHOO.util.Event.onDOMReady</code>, <code class="javascript">YAHOO.util.Event.onContentReady</code> as well as <code class="javascript">YAHOO.util.Event.onAvailable</code> methods. Most of the time, the developer may want certain code to start running as soon as the DOM tree is loaded completely without having to wait for all the elements (image, flash etc.) to finish loading. Then <code class="javascript">YAHOO.util.Event.onDOMReady</code> may come in handy. For example </p>
<p><code class="javascript">YAHOO.util.Event.onDOMReady(function() {<br />
    alert('Good day!');<br />
});</code></p>
<p>After loading the DOM tree, then the browser will prompt &#8216;Good day!&#8217; to the user.</p>
<p>Back to the topic, if the developer wishes to listen to some event and perform action as soon as the event is fired, then he may register the event as follows.</p>
<p><code class="javascript">YAHOO.util.Event.on(some_element, 'change', some_callback_function, extra_parameter);</code></p>
<p>Everything looks fine, but when the developer has structure his code into patterns like the module patttern as follows:</p>
<p><code class="javascript"><br />
(function(Event) {<br />
&nbsp;<br />
var Some_Module = function() {<br />
    return {<br />
        foo: 'bar',<br />
        execute: function() {<br />
            alert(this.foo);<br />
        },<br />
&nbsp;<br />
        listen: function() {<br />
            Event.on(element, 'change', this.execute, null, this);<br />
        }<br />
    };<br />
}();<br />
&nbsp;<br />
})(YAHOO.util.Event);<br />
</code></p>
<p>Notice in the forth and fifth arguments passed into the <code class="javascript">YAHOO.util.Event.on</code> function, I am passing in no extra parameters and the fifth argument defines the scope of <code class="javascript">this</code> keyword in the <code class="javascript">Some_Module.execute</code> method.</p>
<p>If the developer uses <code class="javascript">YAHOO.util.Element</code> to wrap certain elements, then he can also use another way to listen to the event</p>
<p><code class="javascript">var element = YAHOO.util.Element('div');<br />
element.on(some_event, some_callback_function, some_extra_parameter, some_scope);<br />
</code></p>
<p>Speaking of YUI Element, I just recently found a way to &#8216;emulate&#8217; jQuery&#8217;s <code>$()</code> function with YUI components.</p>
<p><code class="javascript"><br />
(function($) {<br />
    $(document.body); // returns the body element wrapped in YAHOO.util.Element class<br />
})(function(_selector) {<br />
    var result = YAHOO.util.Selector.query(_selector);<br />
&nbsp;<br />
    if(result.length > 0) {<br />
        if(result.length == 1) {<br />
            result = new YAHOO.util.Element(result[0]);<br />
        } else {<br />
            for(i in result) {<br />
                result[i] = new YAHOO.util.Element(result[i]);<br />
            }<br />
        }<br />
    } else {<br />
        result = false;<br />
    }<br />
&nbsp;<br />
    return result;<br />
});<br />
</code></p>
<p>Although not as powerful as jQuery&#8217;s $() function, but It can be useful in many situations where YUI is the only choice.</p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/04/11/maintaining-state-with-yui-event/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Random Javascript Notes</title>
		<link>http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/</link>
		<comments>http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 17:24:24 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=100</guid>
		<description><![CDATA[I am starting to like programming in Javascript, especially after understanding more about how Javascript handles scopes. Surprisingly this take me like a year to figure out how scope is managed. The result of the findings is that I start doing a lot of experiments and discovered more interesting stuff through them. It took me [...]]]></description>
			<content:encoded><![CDATA[<p>I am starting to like programming in Javascript, especially after understanding more about how Javascript handles scopes. Surprisingly this take me like a year to figure out how scope is managed. The result of the findings is that I start doing a lot of experiments and discovered more interesting stuff through them.</p>
<p><span id="more-100"></span></p>
<p>It took me quite some time to accept the fact that each function is an object, therefore it is completely normal to write code that looks like</p>
<p><code class="javascript"><br />
var some_function = function() {<br />
    this.public_var = 'hello world';<br />
&nbsp;<br />
    this.public_method = function() {<br />
        // do something<br />
    }<br />
}<br />
&nbsp;<br />
var foo = new some_function();<br />
alert(foo.public_var); // alerts 'hello world'<br />
this.public_method(); // the 'do something' method<br />
</code></p>
<p>Besides, in Javascript, you are allowed to use this magical syntax to automatically call a function after defining it.</p>
<p><code class="javascript"><br />
// without arguments<br />
(function() {<br />
    alert('hello world');<br />
})();<br />
&nbsp;<br />
// with arguments<br />
(function($) {<br />
    $('some parameters'); // where $ = Some_Library_Function as passed below<br />
})(Some_Library_Function);<br />
</code></p>
<p>Because of the fact that a function is an object, you can do some wonderful stuff like this:</p>
<p><code><br />
(function() {<br />
    alert('hello world');<br />
}).call(this); // hello world will be called<br />
</code></p>
<p>As I come from a more C-Like language background (hey, my first language was VC 6.0, what would you expect), I always wanted to find something equivalent in Javascript to define a class. Yes, Javascript is not a OO language in the sense that one can define a class like how it is done in PHP/Java or whatever C-like language. But I can do something like follows:</p>
<p><code class="javascript"><br />
// it is always a good idea to surround code in a small block like this<br />
(function() {<br />
&nbsp;<br />
var Some_Class = function() {<br />
    var private_property;<br />
&nbsp;<br />
    this.public_property;<br />
&nbsp;<br />
    var private_method = function() {<br />
        // do something<br />
    }<br />
&nbsp;<br />
    this.public_method = function() {<br />
        // do something<br />
    }<br />
&nbsp;<br />
    // a cheat to emulate a constructor<br />
    (function() {<br />
        private_property = 'hello world';<br />
        private_method.call(this);<br />
&nbsp;<br />
        this.public_property = 'good day!';<br />
        this.public_method();<br />
    }).call(this);<br />
}<br />
&nbsp;<br />
})();<br />
</code></p>
<p>Yes, I am cheating all the way to make my &#8216;class&#8217; to have a constructor. I can remove the block <code class="javascript">(function() { /* ... */ }).call(this);</code> and everything should still work but I find it looking better if I put the initialization code inside the block.</p>
<p>You may notice I use a lot of <code class="javascript">obj.call()</code> methods in the snippets above. I just found this as well as <code class="javascript">obj.apply()</code> call can actually modify the scope of the <code class="javascript">this</code> keyword. Back to the snippet above, I cannot refer to the property named public property in the private method as follows (without calling the private method using call/apply function):</p>
<p><code class="javascript"><br />
    var private_method = function() {<br />
        alert(this.public_property);<br />
    }<br />
    // somewhere in the code<br />
    private_method(); // this will throw exception as this.public_property is not recognized<br />
    private_method.call(this); // by prividing a scope, then the value of public_property will be displayed<br />
</code></p>
<p>For further readings, I would recommend a <a href="http://odetocode.com/Blogs/scott/archive/2007/07/04/11067.aspx">blog post</a> by <a href="http://odetocode.com/Blogs/scott/default.aspx">K.Scott Allen</a> that discusses the Function.apply and Function.call methods.</p>
<p>Now, after finding a way to change the scope, one may be interested in finding a way to create a closure and has the state maintained. Hence, by collecting the things that I learned in these few months, I am having something as follows:</p>
<p><code class="javascript"><br />
(function(Delegate) {<br />
&nbsp;<br />
var some_callback = function() {<br />
    alert(private_string);<br />
}<br />
var Some_Class = function() {<br />
    var private_string = 'hello world';<br />
&nbsp;<br />
    (function() {<br />
        some_callback = Delegate(some_callback, this);<br />
&nbsp;<br />
        some_callback(); // should alert 'hello world'<br />
    }).call(this);<br />
}<br />
&nbsp;<br />
})(function(_method, _scope) {<br />
    return function() {<br />
        _method.apply(_scope, arguments);<br />
    }<br />
});<br />
</code></p>
<p>Isn&#8217;t this magic? Of course, there are more interesting stuff left for me to find out like inheritance (I am starting to like composition instead of inheritance so this is not a major problem), possibility to define interface in javascript etc. Hopefully this post helps people to quickly get started in Javascript programming.</p>
<p>For further readings, I would suggest the follow up articles of the above linked posts by K. Scott Allen on the <a href="http://odetocode.com/Blogs/scott/archive/2007/07/05/11072.aspx">Delegate</a>, as well as <a href="http://odetocode.com/Blogs/scott/archive/2007/07/09/11077.aspx">closure</a>. I hope I can find some time posting an article on closure once I figure out how to describe the concept without confusing myself and everybody.</p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using YUI for DOM Manipulation</title>
		<link>http://cslai.coolsilon.com/2009/01/16/using-yui-for-dom-manipulation/</link>
		<comments>http://cslai.coolsilon.com/2009/01/16/using-yui-for-dom-manipulation/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 05:07:57 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=87</guid>
		<description><![CDATA[After being frustrated of not getting consistent and accurate result via standard DOM methods especially html_element.getAttribute('key'); and html_element.setAttribute('key', 'value');, I came across some YUI library components that provides abstractions to various DOM methods. Some interesting DOM-related tools covered in this post are YAHOO.util.Element, YAHOO.util.DOM and YAHOO.util.Selector. YAHOO.util.Dom YAHOO.util.Element is basically a singleton instance that provides [...]]]></description>
			<content:encoded><![CDATA[<p>After being frustrated of not getting consistent and accurate result via standard DOM methods especially <code class="javascript">html_element.getAttribute('key');</code> and <code class="javascript">html_element.setAttribute('key', 'value');</code>, I came across some <abbr title="YAHOO! User Interface">YUI</abbr> library components that provides abstractions to various DOM methods. Some interesting DOM-related tools covered in this post are <code class="javascript">YAHOO.util.Element</code>, <code class="javascript">YAHOO.util.DOM</code> and <code class="javascript">YAHOO.util.Selector</code>.</p>
<p><span id="more-87"></span></p>
<h2>YAHOO.util.Dom</h2>
<p><code class="javascript">YAHOO.util.Element</code> is basically a singleton instance that provides various DOM methods. Following are some of the methods that one may find useful. For more information, please refer to the <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Dom.html">API documentation</a>.</p>
<h3>Finding an Element by Id</h3>
<p>To find an element by its Id, instead of document.getElementById, another way through YUI would be through YAHOO.util.Dom.get.</p>
<p><code class="javascript"><br />
var element = YAHOO.util.Dom.get('some_element');<br />
</code></p>
<h3>Finding Element(s) by Class Name</h3>
<p>To find elements by class name, </p>
<p><code class="javascript"><br />
var elements = YAHOO.util.Dom.getElementsByClassName('some_class');<br />
</code></p>
<h2>YAHOO.util.Element</h2>
<p>Before I discover how powerful is jQuery, I have relied on <code class="javascript">YAHOO.util.Element</code> a lot in various tasks. Basically, <code class="javascript">YAHOO.util.Element</code> wraps a HTMLElement and provide some methods to simplify the code used in certain DOM operations such as getting and setting attributes, manipulating element position etc. Detailed API documentation is <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Element.html">here</a>.</p>
<h3>Creating a New Element</h3>
<p>To create a new element, one typically pass in the result from <code class="javascript">document.createElement</code> into the constructor as follows. For example, to create a new link element.</p>
<p><code class="javascript"><br />
// create a new element<br />
var element = new YAHOO.util.Element(document.createElement('a'));<br />
&nbsp;<br />
// set the href<br />
element.set('href', 'http://www.google.com/');<br />
&nbsp;<br />
// insert caption<br />
element.appendChild(document.createTextNode(<br />
    'Click me to the search engine.'<br />
));<br />
&nbsp;<br />
element.appendTo(document.body);<br />
</code></p>
<h3>Uses an existing Element</h3>
<p>To wrap YUI Element component methods to an existing HTMLElement, pass in the element OR the element Id into the constructor.</p>
<p><code class="javascript"><br />
var by_element = new YAHOO.util.Element(<br />
    YAHOO.util.Dom.get('some_id')<br />
);<br />
var by_id = new YAHOO.util.Element('some_id');<br />
</code></p>
<h3>Getting the original HTMLElement</h3>
<p>Sooner or later one would find out that it is impossible to call some DOM methods through the <code class="javascript">YAHOO.util.Element</code> instance, for instance the <code class="javascript">setAttribute</code> method. Besides that, as of 2.6.0, the appendChild method also doesn&#8217;t seem to accept instance from <code class="javascript">YAHOO.util.Element</code>. Therefore to get the wrapped element,</p>
<p><code class="javascript"><br />
var some_container = new YAHOO.util.Element(container_element);<br />
var the_yui_element = new YAHOO.util.Element(original_element);<br />
&nbsp;<br />
// this will result in an error<br />
some_container.appendChild(the_yui_element);<br />
&nbsp;<br />
// but this won't<br />
some_container.appendChild(the_yui_element.get('element'));<br />
</code></p>
<p>Hopefully instances of <code class="javascript">YAHOO.util.Element</code> are also accepted by all methods requiring HTML element in future releases.</p>
<h2>YAHOO.util.Selector</h2>
<h3>DOM Collection via CSS3 Selector</h3>
<p>Those who are from jQuery may be interested in knowing that YUI also provides similar functionality via <code class="javascript">YAHOO.util.Selector</code>. For those who are interested, the API documentation is <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Selector.html">here</a>. As an example,</p>
<p><code class="javascript"><br />
var elements = YAHOO.util.Selector.query('body p');<br />
</code></p>
<p>For those who are interested, the <a href="http://developer.yahoo.com/yui/">official site</a> provides a great amount of information, guide, examples and tutorials. However, for those who are new to javascript, the tutorials may take them some time to get used to. However, after investing enough time in it, then the extensive <a href="http://developer.yahoo.com/yui/docs/">API documentation</a> will be extremely useful. I will document down some notes on <code class="javascript">YAHOO.util.Event</code>, which is my favorite component next time.</p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/01/16/using-yui-for-dom-manipulation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Random Color Cloud</title>
		<link>http://cslai.coolsilon.com/2009/01/09/random-color-cloud/</link>
		<comments>http://cslai.coolsilon.com/2009/01/09/random-color-cloud/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 07:08:04 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=79</guid>
		<description><![CDATA[Just happened to see this post a few months ago, and the author created another cloud that uses almost the same technique to &#8216;visualize&#8217; a list of countries. The author uses PHP to generate the cloud originally and I thought I may be able to do in javascript. After some quick coding I managed to [...]]]></description>
			<content:encoded><![CDATA[<p>Just happened to see <a href="http://www.reinholdweber.com/?p=36">this post</a> a few months ago, and the author created another cloud that uses almost the same technique to &#8216;visualize&#8217; a list of countries. The author uses PHP to generate the cloud originally and I thought I may be able to do in javascript. After some quick coding I managed to produce something similar to the first example, source code after the jump.</p>
<p><span id="more-79"></span></p>
<p><code class="html"><br />
&lt;html&gt;<br />
    &lt;head&gt;<br />
        &lt;style&gt;<br />
            span:hover { cursor: pointer; font-size: 400%; }<br />
        &lt;/style&gt;<br />
    &lt;/head&gt;<br />
    &lt;body&gt;<br />
        &lt;script&gt;<br />
            for(var i = 0; i < 1000; i++) {<br />
                var span = document.createElement('span');<br />
                span.setAttribute(<br />
                    'style',<br />
                    'position: absolute; ' +<br />
                    'left: ' +<br />
                        Math.round(Math.random() * 90 % 90).toString() +<br />
                    '%; ' +<br />
                    'top: ' +<br />
                        Math.round(Math.random() * 90 % 90).toString() +<br />
                    '%;' +<br />
                    'color: rgb(' +<br />
                        Math.round(Math.random() * 256 % 256).toString() + ',' +<br />
                        Math.round(Math.random() * 256 % 256).toString() + ',' +<br />
                        Math.round(Math.random() * 256 % 256).toString() +<br />
                    '); '<br />
                );<br />
                span.setAttribute(<br />
                    'class', 'c'<br />
                );<br />
                span.appendChild(document.createTextNode(<br />
                    Math.round(Math.random() * 256 % 256).toString()<br />
                ));<br />
                document.body.appendChild(span);<br />
            }<br />
        &lt;/script&gt;<br />
    &lt;/script&gt;&lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>Too lazy to produce a preview page, but it should look somewhat like the <a href="http://reinholdweber.com/colors.php">original page</a>, except that the caption isn't related to the color. The above script is not tested on other browsers besides Firefox 3, so they may break (I am extremely suck at Javascript hence I rely on <a href="http://developer.yahoo.com/yui/">YUI</a> a lot, hopefully get to writeup on YUI soon)</code></p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/01/09/random-color-cloud/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working Life</title>
		<link>http://cslai.coolsilon.com/2008/05/04/working-life/</link>
		<comments>http://cslai.coolsilon.com/2008/05/04/working-life/#comments</comments>
		<pubDate>Sun, 04 May 2008 08:08:31 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=43</guid>
		<description><![CDATA[I didn&#8217;t realize that I have been working for 3 weeks until the Labour Day which was a public holiday. Many things happened in these few weeks and I am still struggling to catch up with it. My superior and colleagues have been very helpful and offered me some helpful tutorials and books. I was [...]]]></description>
			<content:encoded><![CDATA[<p>I didn&#8217;t realize that I have been working for 3 weeks until the Labour Day which was a public holiday. Many things happened in these few weeks and I am still struggling to catch up with it. My superior and colleagues have been very helpful and offered me some helpful tutorials and books. I was instructed to build a event scheduler application using <a href="http://codeigniter.com">codeigniter</a> in the first week and then work on a side project that extends a form using DOM methods and properties.</p>
<p><span id="more-43"></span></p>
<p>Developing the scheduler was not too difficult as I do not have to deal with browser experience inconsistencies very much and only concentrate on the business logic. However, when I started on my second project that extends a form to provide various widgets to guide users in filling the form correctly using <a href="http://developer.yahoo.com/yui/">YUI</a> I nearly killed myself due to frustration. I am developing my scripts in a Ubuntu Gutsy Gibbon 7.10 desktop and use firefox 3 beta 4 to test them. I finally managed to produce a workable prototype in a week that runs almost perfectly under firefox.</p>
<p>Nightmare comes when the same script is deployed to a windows machine with the major 4 browsers installed (firefox, safari, opera and internet explorer). All browsers except IE7 runs my script and the error message offered by IE7 has no meaning. The first problem encountered was with the select box creation through DOM. As suggested by a tutorial in <a href="http://www.w3schools.com/htmldom/met_select_add.asp">w3school</a>, I used the following code to create the selection box.</p>
<p><code class="javascript">var y=document.createElement('option');<br />
y.text='Kiwi'<br />
var x=document.getElementById("mySelect");<br />
try {<br />
    x.add(y,null); // standards compliant<br />
}<br />
catch(ex) {<br />
    x.add(y); // IE only<br />
}</code></p>
<p>Internet Explorer was not giving any helpful error message telling me which part is not right and I would have to use the following code to achieve the same thing.</p>
<p><code class="javascript">x.options.add(new Option(...))</code></p>
<p>The reference of the constructor parameters to instantiate an Option object can be found <a href="http://www.javascriptkit.com/jsref/select.shtml">here</a>.</p>
<p>There are a couple of other problems too but fortunately I managed to fixed it in time. Internet Explorer is very strict with DOM but the problem with it is that the error message it spits is often far from being helpful. How the hell can one find out what can possibly be the cause of a &#8220;random runtime error&#8221;?</p>
<p>Recently I started to learn how to code using VIM through <a href="http://cream.sf.net/">CREAM</a> and vimtutor. I am now in the progress of remembering the key commands and probably uninstall cream by the end of the week to fully switch to VIM. I used to code in a mini-emacs editor provided by swi-prolog when I was doing my prolog subject but I didn&#8217;t quite like it because I don&#8217;t have a very flexible fingers to press the modifier keys all the time. <img src='http://cslai.coolsilon.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2008/05/04/working-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slideshow using Scriptaculous</title>
		<link>http://cslai.coolsilon.com/2008/03/24/slideshow-using-scriptaculous/</link>
		<comments>http://cslai.coolsilon.com/2008/03/24/slideshow-using-scriptaculous/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 16:04:09 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Scriptaculuous]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=13</guid>
		<description><![CDATA[I need a slide show script for my portfolio pages but couldn&#8217;t find a good one anywhere so I decided to write one myself. The slide show script will be able to display image and the respective description in a predefined order. However, in this version, visitors would not be able to directly jump to [...]]]></description>
			<content:encoded><![CDATA[<p>I need a slide show script for my portfolio pages but couldn&#8217;t find a good one anywhere so I decided to write one myself. The slide show script will be able to display image and the respective description in a predefined order. However, in this version, visitors would not be able to directly jump to a particular slide yet. The script is written in <a href="http://www.prototypejs.org/learn/class-inheritance">prototype</a>&#8216;s object-orientation approach hence you need to have prototype called.</p>
<p><span id="more-13"></span></p>
<h2>Prerequisite</h2>
<ol>
<li>Latest <a href="http://prototypejs.org/">Prototype</a> Javascript Framework and <a href="http://script.aculo.us/">Scrit.aculo.us</a> library</li>
<li>Call the slide show script file which you can download from <a href="http://cslai.coolsilon.com/wp-content/themes/sim-kite/javascripts/user/slide.js">here</a></li>
<li>
		Insert the images into a a list with id <code>olImageSlide</code> in following format:<br />
		<code class="html"><br />
			&lt;ol id="olImageSlide"&gt;<br />
				&lt;li id="liImageSlide1"&gt;&lt;!--Insert image here-&gt;&lt;/li&gt;<br />
				&lt;li id="liImageSlide2"&gt;&lt;!--Insert image here-&gt;&lt;/li&gt;<br />
			&lt;/ol&gt;<br />
		</code><br />
		Notice the increment of value in the list element id.
	</li>
<li>
		Insert the images into a a list with id <code>olDescriptionSlide</code> in following format:<br />
		<code class="html"><br />
			&lt;ol id="olDescriptionSlide"&gt;<br />
				&lt;li id="liDescriptionSlide1"&gt;&lt;!--Insert Description here-&gt;&lt;/li&gt;<br />
				&lt;li id="liDescriptionSlide2"&gt;&lt;!--Insert Description here-&gt;&lt;/li&gt;<br />
			&lt;/ol&gt;<br />
		</code><br />
		Notice the increment of value in the list element id.
	</li>
<li>Done!</li>
</ol>
<h2>Demo</h2>
<p><script type="text/javascript" src="http://cslai.coolsilon.com/wp-content/themes/sim-kite/javascripts/user/slide.js"></script></p>
<div id="divSlideShow" style="height: 220px;">
<ol id="olImageSlide">
<li id="liImageSlide1"><img src="http://cslai.coolsilon.com/wp-content/uploads/cslai/2008/03/example-slide-1.png" alt="The example slide 1" title="example-slide-1" width="200" height="200" /></li>
<li id="liImageSlide2"><img src="http://cslai.coolsilon.com/wp-content/uploads/cslai/2008/03/example-slide-2.png" alt="The example slide 2" title="example-slide-2" width="200" height="200" /></li>
</ol>
<ol id="olDescriptionSlide">
<li id="liDescriptionSlide1">
<h4>Slide 1</h4>
<p>This is the description for the first slide</p>
</li>
<li id="liDescriptionSlide2">
<h4>Slide 2</h4>
<p>This is the description for the second slide and is relatively longer</p>
</li>
</ol>
</div>
<h2>Note</h2>
<p>It is advisible to have all the image cropped into the same size.</p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2008/03/24/slideshow-using-scriptaculous/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

