<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Programming collection</title>
	<atom:link href="http://terenceyim.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://terenceyim.wordpress.com</link>
	<description>Programming collection</description>
	<lastBuildDate>Sat, 10 Jan 2009 00:40:02 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
		<url>http://www.gravatar.com/blavatar/02e148cee44f92fa9d47a9491329b616?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Programming collection</title>
		<link>http://terenceyim.wordpress.com</link>
	</image>
			<item>
		<title>Adding ping() function to PDO</title>
		<link>http://terenceyim.wordpress.com/2009/01/09/adding-ping-function-to-pdo/</link>
		<comments>http://terenceyim.wordpress.com/2009/01/09/adding-ping-function-to-pdo/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 00:36:45 +0000</pubDate>
		<dc:creator>terenceyim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://terenceyim.wordpress.com/?p=36</guid>
		<description><![CDATA[Recently helped my colleague to add the missing mysqli_ping() function to PDO object in PHP. If anyone missed it, here it is:
&#60;?php
class NPDO {
    private $pdo;
    private $params;

    public function __construct() {
        $this-&#62;params = func_get_args();
     [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=36&subd=terenceyim&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Recently helped my colleague to add the missing <a href="http://us3.php.net/manual/en/mysqli.ping.php"><code>mysqli_ping()</code></a> function to PDO object in PHP. If anyone missed it, here it is:</p>
<pre>&lt;?php
class NPDO {
    private $pdo;
    private $params;

    public function __construct() {
        $this-&gt;params = func_get_args();
        $this-&gt;init();
    }

    public function __call($name, array $args) {
        return call_user_func_array(array($this-&gt;pdo, $name), $args);
    }

    <span style="color:#339966;">// The ping() will try to reconnect once if connection lost.
</span>    public function ping() {
        try {
            $this-&gt;pdo-&gt;query('SELECT 1');
        } catch (PDOException $e) {
            $this-&gt;init();  <span style="color:#339966;">// Don't catch exception here, so that re-connect fail will throw exception</span>
        }

        return true;
    }

    private function init() {
        $class = new ReflectionClass('PDO');
        $this-&gt;pdo = $class-&gt;newInstanceArgs($this-&gt;params);
    }
}
?&gt;</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terenceyim.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terenceyim.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terenceyim.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terenceyim.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terenceyim.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terenceyim.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terenceyim.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terenceyim.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terenceyim.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terenceyim.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=36&subd=terenceyim&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://terenceyim.wordpress.com/2009/01/09/adding-ping-function-to-pdo/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6ca44c6e23cffd81d5078114ea302184?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Terence</media:title>
		</media:content>
	</item>
		<item>
		<title>PHP Asynchronous multiple HTTP caller class</title>
		<link>http://terenceyim.wordpress.com/2008/12/12/19/</link>
		<comments>http://terenceyim.wordpress.com/2008/12/12/19/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 00:55:33 +0000</pubDate>
		<dc:creator>terenceyim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[PHP HTTP "Web service" asynchronous]]></category>

		<guid isPermaLink="false">http://terenceyim.wordpress.com/?p=19</guid>
		<description><![CDATA[Today written a PHP class for handling multiple HTTP calls asynchronously (current only supports GET, feel free to modify it to support POST and PUT).  By saying asynchronous, it can do something like this:
&#60;?php
    $caller = new HttpCaller();
    $id1 = $caller-&#62;prepare($url1);
    $id2 = $caller-&#62;prepare($url2);
  [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=19&subd=terenceyim&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Today written a PHP class for handling multiple HTTP calls <strong>asynchronously</strong> (current only supports GET, feel free to modify it to support POST and PUT).  By saying asynchronous, it can do something like this:</p>
<pre>&lt;?php
    $caller = new HttpCaller();
    $id1 = $caller-&gt;prepare($url1);
    $id2 = $caller-&gt;prepare($url2);
    $id3 = $caller-&gt;prepare($url3);

    <span style="color:#339966;">// Send all requests and return immediately</span>
    $caller-&gt;makeCall();

    <span style="color:#339966;">// Doing something else here (fetching data from DB, computing... all sorts of stuff</span>

    <span style="color:#339966;">// Now we need the result for $url2 call, block until $url2 call return, but no need to wait for $url1 or $url3</span>
    $result = $caller-&gt;fetch($id2);
    <span style="color:#339966;">// Do something with $result and maybe something else
</span>
    $result = $caller-&gt;fetch($id1);    <span style="color:#339966;">// Need the result for $url1 call</span>
    <span style="color:#339966;">// Do something with $result and maybe something else
</span>
    $result = $caller-&gt;fetch($id3);
    <span style="color:#339966;">// Do something with $result</span>
?&gt;</pre>
<p>Implementation of HttpCaller:</p>
<pre>&lt;?php
    class HttpCaller {
        private $timeout;
        private $mutliCurl;
        private $resultCache = array();

        public function __construct($timeout = 9999) {
            $this-&gt;timeout = intval($timeout);
            $this-&gt;multiCurl = curl_multi_init();
        }

        public function __destruct() {
            curl_multi_close($this-&gt;multiCurl);
        }

        public function prepare($url) {
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, $this-&gt;timeout);

            curl_multi_add_handle($this-&gt;multiCurl, $curl);

            return intval($curl);
        }

        public function makeCall() {
            do {
                $res = curl_multi_exec($this-&gt;multiCurl, $isRunning);
            } while ($res == CURLM_CALL_MULTI_PERFORM);
        }

        public function fetch($id) {
            if (isset($this-&gt;resultCache[$id])) {
                return $this-&gt;resultCache[$id];
            }

            do {
                if (curl_multi_select($this-&gt;multiCurl) != -1) {
                    do {
                        $res = curl_multi_exec($this-&gt;multiCurl, $active);
                    } while ($res == CURLM_CALL_MULTI_PERFORM);

                    $info = curl_multi_info_read($this-&gt;multiCurl);
                    if ($info) {
                        if (curl_errno($info["handle"]) !== 0) {
                            return false;
                        }
                        $tmp = intval($info["handle"]);
                        $this-&gt;resultCache[$tmp] = curl_multi_getcontent($info["handle"]);

                        if ($tmp === $id) {
                            return $this-&gt;resultCache[$id];
                        }
                    }
                }
            } while ($active &amp;&amp; $res == CURLM_OK);
        }
    }
?&gt;</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terenceyim.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terenceyim.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terenceyim.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terenceyim.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terenceyim.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terenceyim.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terenceyim.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terenceyim.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terenceyim.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terenceyim.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=19&subd=terenceyim&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://terenceyim.wordpress.com/2008/12/12/19/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6ca44c6e23cffd81d5078114ea302184?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Terence</media:title>
		</media:content>
	</item>
		<item>
		<title>Stupid programming</title>
		<link>http://terenceyim.wordpress.com/2008/12/12/stupid-programming/</link>
		<comments>http://terenceyim.wordpress.com/2008/12/12/stupid-programming/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 00:37:00 +0000</pubDate>
		<dc:creator>terenceyim</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript "bad programming"]]></category>

		<guid isPermaLink="false">http://terenceyim.wordpress.com/?p=15</guid>
		<description><![CDATA[Just come across a stupid piece of JS program today in some web site:
function preselectSS(what)
{
  if (what == 1) document.getElementById("selSS").selectedIndex = 0;
  else if (what == 2) document.getElementById("selSS").selectedIndex = 1;
  else if (what == 3) document.getElementById("selSS").selectedIndex = 2;
  else document.getElementById("selSS").selectedIndex = 3;
}
Isn&#8217;t it a lot nicer to write it as
function preselectSS(what) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=15&subd=terenceyim&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Just come across a stupid piece of JS program today in some web site:</p>
<pre>function preselectSS(what)
{
  if (what == 1) document.getElementById("selSS").selectedIndex = 0;
  else if (what == 2) document.getElementById("selSS").selectedIndex = 1;
  else if (what == 3) document.getElementById("selSS").selectedIndex = 2;
  else document.getElementById("selSS").selectedIndex = 3;
}</pre>
<p>Isn&#8217;t it a lot nicer to write it as</p>
<pre>function preselectSS(what) {
    document.getElementById("selSS").selectedIndex = (what &gt; 0 &amp;&amp; what &lt;= 3) ? (what - 1) : 3;
}</pre>
<p>The world is full of stupid programmers writing stupid programs.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terenceyim.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terenceyim.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terenceyim.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terenceyim.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terenceyim.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terenceyim.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terenceyim.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terenceyim.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terenceyim.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terenceyim.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=15&subd=terenceyim&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://terenceyim.wordpress.com/2008/12/12/stupid-programming/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6ca44c6e23cffd81d5078114ea302184?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Terence</media:title>
		</media:content>
	</item>
		<item>
		<title>Cross domain connection manager with same calling mechanism as YUI</title>
		<link>http://terenceyim.wordpress.com/2008/01/02/cross-domain-connection-manager-with-same-calling-mechanism-as-yui/</link>
		<comments>http://terenceyim.wordpress.com/2008/01/02/cross-domain-connection-manager-with-same-calling-mechanism-as-yui/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 08:24:05 +0000</pubDate>
		<dc:creator>terenceyim</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[asyncRequest]]></category>
		<category><![CDATA[connection]]></category>
		<category><![CDATA[Cross domain]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://terenceyim.wordpress.com/2008/01/02/cross-domain-connection-manager-with-same-calling-mechanism-as-yui/</guid>
		<description><![CDATA[Update: The same functionality is now provided by the Yahoo! YUI Get Utility.
The YUI connection manager use XMLHttpRequest to make remote call, hence calling cross domain URL is prohibited by the browser security model. This class is to solve this situation without the need to setup any server side proxy, while maintaining the same calling [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=12&subd=terenceyim&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>Update: </strong>The same functionality is now provided by the Yahoo! <a href="http://developer.yahoo.com/yui/get/">YUI Get Utility</a>.</p>
<p>The <a href="http://developer.yahoo.com/yui/connection/">YUI connection manager</a> use <code>XMLHttpRequest</code> to make remote call, hence calling cross domain URL is prohibited by the browser security model. This class is to solve this situation without the need to setup any server side proxy, while maintaining the same calling mechanism as the YUI connection manager.</p>
<p><strong>Script source:</strong> <span style="text-decoration:line-through;"><a href="http://rockstonesand.com/js/crossdomainmanager.js">http://rockstonesand.com/js/crossdomainmanager.js</a></span></p>
<p><strong>Minified version:</strong> <span style="text-decoration:line-through;"><a href="http://rockstonesand.com/js/crossdomainmanager_min.js">http://rockstonesand.com/js/crossdomainmanager_min.js</a></span></p>
<p><strong>Usage:</strong> Same as <a href="http://developer.yahoo.com/yui/connection/#async">YUI connection manager</a></p>
<p><strong>Functions supported:</strong></p>
<pre>asyncRequest, isCallInProgress, abort</pre>
<p><strong>Example:</strong><br />
<em>Client side:</em></p>
<div style="border:1px solid grey;">
<pre><span style="color:#008000;">&lt;script src="http://rockstonesand.com/js/crossdomainmanager_min.js"&gt;&lt;/script&gt;
&lt;script&gt;
// Same callback object definition as YUI connection manager
var callback = {
    success: function(obj) {
        var response = obj.responseText;
        // Do UI updates
    },
    failure: function(obj) {
    }
};
var txId = CrossDomainConnect.asyncRequest(
   "http://api.flickr.com/services/feeds/photos_public.gne?lang=en-us&amp;format=json",
   callback,
   "jsoncallback");
&lt;/script&gt;</span></pre>
</div>
<p><strong>Limitations:</strong></p>
<ul>
<li>Only GET method is supported, as it uses script tag hack</li>
<li>The target server side script must support an additional &#8220;callback&#8221; parameter for the connection manager to specify a callback function</li>
</ul>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terenceyim.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terenceyim.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terenceyim.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terenceyim.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terenceyim.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terenceyim.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terenceyim.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terenceyim.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terenceyim.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terenceyim.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terenceyim.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terenceyim.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=12&subd=terenceyim&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://terenceyim.wordpress.com/2008/01/02/cross-domain-connection-manager-with-same-calling-mechanism-as-yui/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6ca44c6e23cffd81d5078114ea302184?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Terence</media:title>
		</media:content>
	</item>
		<item>
		<title>The Bubbling Technique &amp; Custom Event in YUI</title>
		<link>http://terenceyim.wordpress.com/2007/10/25/the-bubbling-technique-custom-event-in-yui/</link>
		<comments>http://terenceyim.wordpress.com/2007/10/25/the-bubbling-technique-custom-event-in-yui/#comments</comments>
		<pubDate>Thu, 25 Oct 2007 15:35:53 +0000</pubDate>
		<dc:creator>terenceyim</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://terenceyim.wordpress.com/2007/10/25/the-bubbling-technique-custom-event-in-yui/</guid>
		<description><![CDATA[Good framework for applying MVC design in Web Application.
http://yuiblog.com/blog/2007/09/13/bubbling-library-by-caridy/
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=9&subd=terenceyim&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Good framework for applying MVC design in Web Application.</p>
<p><a href="http://yuiblog.com/blog/2007/09/13/bubbling-library-by-caridy/">http://yuiblog.com/blog/2007/09/13/bubbling-library-by-caridy/</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terenceyim.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terenceyim.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terenceyim.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terenceyim.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terenceyim.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terenceyim.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terenceyim.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terenceyim.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terenceyim.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terenceyim.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terenceyim.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terenceyim.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=9&subd=terenceyim&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://terenceyim.wordpress.com/2007/10/25/the-bubbling-technique-custom-event-in-yui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6ca44c6e23cffd81d5078114ea302184?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Terence</media:title>
		</media:content>
	</item>
		<item>
		<title>Javascript that crashes IE</title>
		<link>http://terenceyim.wordpress.com/2007/10/09/javascript-that-crashes-ie/</link>
		<comments>http://terenceyim.wordpress.com/2007/10/09/javascript-that-crashes-ie/#comments</comments>
		<pubDate>Tue, 09 Oct 2007 03:49:19 +0000</pubDate>
		<dc:creator>terenceyim</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[IE crash]]></category>
		<category><![CDATA[script tag hack]]></category>

		<guid isPermaLink="false">http://terenceyim.wordpress.com/2007/10/09/javascript-that-crashes-ie/</guid>
		<description><![CDATA[Recently I was helping my colleague to fix a strange bug in his Javascript. The bug behave like this:

Open the web page in IE.
After everything are loaded (HTML, images, scripts, &#8230;), click on address bar and press enter
IE crashed  

When I try to identify the bug, I&#8217;ve created a simple page that can reproduce [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=4&subd=terenceyim&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Recently I was helping my colleague to fix a strange bug in his Javascript. The bug behave like this:</p>
<ol>
<li>Open the web page in IE.</li>
<li>After everything are loaded (HTML, images, scripts, &#8230;), click on address bar and press enter</li>
<li>IE crashed <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </li>
</ol>
<p>When I try to identify the bug, I&#8217;ve created a simple page that can reproduce the bug.</p>
<p><font color="#ff0000">crash.html</font></p>
<pre><font color="#339966">&lt;<span class="start-tag">div</span><span class="attribute-name"> id</span>=<span class="attribute-value">"panel"</span>&gt;&lt;/<span class="end-tag">div</span>&gt;
&lt;<span class="start-tag">a</span><span class="attribute-name"> href</span>=<span class="attribute-value">"#" </span><span class="attribute-name">onclick</span>=<span class="attribute-value">"loadScript(); return false;"</span>&gt;Load script&lt;/<span class="end-tag">a</span>&gt;

&lt;<span class="start-tag">script</span>&gt;
    var n = 0;
    var callback = function() {
        n++;
        var panel = document.getElementById("panel");
        panel.innerHTML = "loadScript called " + n + " times.";
        var node = document.getElementById("testid");
        node.parentNode.removeChild(node);
    }

    var loadScript = function() {
        var node = document.createElement("script");
        node.src="crash.js";
        node.id = "testid";

        document.getElementsByTagName("head")[0].appendChild(node);
    }

    window.onload = loadScript;
&lt;/<span class="end-tag">script</span>&gt;</font></pre>
<p><span id="more-4"></span><font color="#ff0000"> crash.js</font></p>
<pre><!--more--><font color="#339966">callback();</font></pre>
<p><!--more-->What the page does is to create a script node dynamically and insert into the document to load an external .js file. When the external &#8220;crash.js&#8221; is loaded, it calls a function to update the page content and remove the script node. IE will be crashed by either click on the address and hit enter button or just click on the link &#8220;Load script&#8221;.</p>
<p>The actual reason why IE crashes on this situation is unknown, but one thing can be sure is it only crashes if the same URL is being requested. If you try to append a dummy random query to the script src URL, IE is working fine.</p>
<p>I&#8217;ve also discovered another way, which I think is better in solving this problem. Simply use a setTimeout() function to wrap around the removeChild statement with timeout = 0.</p>
<pre><!--more--><font color="#339966">setTimeout(function() {
</font><font color="#339966">    var node = document.getElementById("testid");
    node.parentNode.removeChild(node);
}, 0);</font></pre>
<p>The problem will be gone by doing this.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terenceyim.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terenceyim.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terenceyim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terenceyim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terenceyim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terenceyim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terenceyim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terenceyim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terenceyim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terenceyim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terenceyim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terenceyim.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terenceyim.wordpress.com&blog=1872500&post=4&subd=terenceyim&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://terenceyim.wordpress.com/2007/10/09/javascript-that-crashes-ie/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6ca44c6e23cffd81d5078114ea302184?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Terence</media:title>
		</media:content>
	</item>
	</channel>
</rss>