<?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>The Fastest Site on The Block Blog - GetSnappy.com</title>
	<atom:link href="http://www.getsnappy.com/blog/wp-feed.php" rel="self" type="application/rss+xml" />
	<link>http://www.getsnappy.com/blog</link>
	<description>The Fastest Site on The Block</description>
	<lastBuildDate>Thu, 12 Nov 2009 22:14:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Don&#8217;t Let Ads, Widgets, and other External JavaScript Slow You Down</title>
		<link>http://www.getsnappy.com/blog/web-optimization-page-load-time/dont-let-ads-widgets-and-other-external-javascript-slow-you-down/</link>
		<comments>http://www.getsnappy.com/blog/web-optimization-page-load-time/dont-let-ads-widgets-and-other-external-javascript-slow-you-down/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 18:47:45 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Website Optimization]]></category>

		<guid isPermaLink="false">http://www.getsnappy.com/blog/?p=19</guid>
		<description><![CDATA[Ever heard the expression fight fire with fire.  Well, for this web optimization technique, we will be fighting JavaScript with JavaScript.  This technique we call postload swapping allows you to load these JavaScript files inside a hidden DIV at the very bottom of your web page, then use JavaScript to swap out the the contents created by document.write calls inside the hidden DIV with contents of a placeholder element where the JavaScript was originally located.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px 30px 10px 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.getsnappy.com%2Fblog%2Fweb-optimization-page-load-time%2Fdont-let-ads-widgets-and-other-external-javascript-slow-you-down%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.getsnappy.com%2Fblog%2Fweb-optimization-page-load-time%2Fdont-let-ads-widgets-and-other-external-javascript-slow-you-down%2F" height="61" width="51" /></a></div><h4>Why are JavaScript calls a problem</h4>
<p>As the web evolves and more and more sites move towards being &#8220;web 2.0&#8243; applications, JavaScript is used more and in greater volume.  This is a major problem because most browsers including IE and Firefox block loading and rendering elements below script tags, until the script has been downloaded and executed.  <a href="http://www.stevesounders.com/">Steve Sounders</a> wrote a <a href="http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/">killer blog about preventing blocking of JavaScript calls</a>, but he didn&#8217;t address the situation where scripts call document.write() to inject HTML immediately below the script tag.  This <a href="/blog/javascript-test/">test page</a> shows that using the Script DOM Element technique causes document.write calls from within the external script to output html at undesirable places.  This blog introduces a new technique called &#8220;postload dummy replacement&#8221;, that specifically addresses loading scripts that rely on document.write().</p>
<h4>Who uses External JavaScript</h4>
<p>You can pick up external JavaScript from many different sources.  It is commonly used by ad agencies, widgets, and third party embeddable objects.  For instance, a VeriSign logo that let&#8217;s people know your site is secure and a Skype button I recently picked up to allow visitors to see when I&#8217;m online and skype me, both use externally loaded JavaScript that rely on document.write().  Ad Agencies can even nest external JavaScript within external JavaScript leaving you at the mercy of multiple third party providers.</p>
</p>
<h4>Fighting JavaScript with JavaScript</h4>
<p>Ever heard the expression fight fire with fire.  Well, for this web optimization technique, we will be fighting JavaScript with JavaScript.  This technique I call &#8220;postload dummy replacement&#8221; allows you to load these JavaScript files inside a hidden DIV at the very bottom of your web page, then use JavaScript to swap out the the contents created by document.write calls inside the hidden DIV with contents of a placeholder element where the JavaScript was originally located.</p>
<h4>GetSnappy.com Ad Delay</h4>
<p>Ad Delay, is a JavaScript module I&#8217;ve written that implements &#8220;postload dummy replacement&#8221;.  I&#8217;ll introduce you to Ad Delay, by walking through an example.  I&#8217;ll use the Skype button mentioned above in my example.  We&#8217;ll start by creating a placeholder DIV and set it&#8217;s id to &#8220;skypePlaceholder&#8221;. </p>
<pre><tt><b><font color="#0000FF">&lt;div&gt;&lt;div</font></b> <font color="#009900">id</font><font color="#990000">=</font><font color="#FF0000">"skypeUsPlaceholder"</font><b><font color="#0000FF">&gt;&lt;/div&gt;&lt;/div&gt;</font></b>
</tt></pre>
<p>While &#8220;skypeUsPlaceholder&#8221; is empty, you are free to implement placeholders however you please, and there contents will be replaced by the call to adDelay.swapOut() mentioned in the last step.  You might want to set a height and width for the placeholder, to prevent the page from &#8220;jumping&#8221; when the swapOut occurs, and you could even include an Ajax loading icon, such as the ones available from <a href="http://www.ajaxload.info/">http://www.ajaxload.info/</a>.</p>
<p>Now let&#8217;s jump down to the bottom of our web page, immediately before the closing &lt;/body&gt; tag.  Here we will include the JavaScript tag within a &#8220;hidden&#8221; dummy DIV.  We will assign this DIV the id &#8220;skypeDummy&#8221;.  When the script executes document.write() calls fill the &#8220;hidden&#8221; DIV with the intended content.</p>
<pre><tt><b><font color="#0000FF">&lt;div</font></b> <font color="#009900">id</font><font color="#990000">=</font><font color="#FF0000">"skypeUsDummy"</font> <font color="#009900">style</font><font color="#990000">=</font><font color="#FF0000">"display: none;"</font><b><font color="#0000FF">&gt;</font></b>
  <i><font color="#9A1900">&lt;!--</font></i>
<i><font color="#9A1900">  Externally loaded JavaScript code</font></i>
<i><font color="#9A1900">  --&gt;</font></i>
  <b><font color="#0000FF">&lt;script</font></b> <font color="#009900">type</font><font color="#990000">=</font><font color="#FF0000">"text/javascript"</font>
          <font color="#009900">src</font><font color="#990000">=</font><font color="#FF0000">"http://download.skype.com/share/skypebuttons/js/skypeCheck.js"</font><b><font color="#0000FF">&gt;</font></b>
  <b><font color="#0000FF">&lt;/script&gt;</font></b>
  <b><font color="#0000FF">&lt;a</font></b> <font color="#009900">href</font><font color="#990000">=</font><font color="#FF0000">"skype:GetSnappy.com?chat"</font><b><font color="#0000FF">&gt;</font></b>
  <b><font color="#0000FF">&lt;img</font></b> <font color="#009900">src</font><font color="#990000">=</font><font color="#FF0000">"http://download.skype.com/share/skypebuttons/buttons/chat_green_white_164x52.png"</font>
       <font color="#009900">style</font><font color="#990000">=</font><font color="#FF0000">"border: none;"</font> <font color="#009900">width</font><font color="#990000">=</font><font color="#FF0000">"164"</font> <font color="#009900">height</font><font color="#990000">=</font><font color="#FF0000">"52"</font> <font color="#009900">alt</font><font color="#990000">=</font><font color="#FF0000">"Chat with me"</font> <b><font color="#0000FF">/&gt;</font></b>
  <b><font color="#0000FF">&lt;/a&gt;</font></b>
<b><font color="#0000FF">&lt;/div&gt;</font></b>
</tt></pre>
<p>Next we&#8217;ll unveil the source for Ad Delay which should be included at the very bottom of your web page.  Ad Delay is a tiny JavaScript module containing one function, adDelay.swapOut.</p>
<pre><tt><i><font color="#9A1900">/*</font></i>
<i><font color="#9A1900"> * Name: Ad Delay v0.1 </font></i>
<i><font color="#9A1900"> * License:  GNUv2 (www.gnu.org/licenses/gpl-2.0.txt)</font></i>
<i><font color="#9A1900"> * Auther: Brian Gardner </font></i><u><font color="#0000FF">&lt;getsnappy@ymail.com&gt;</font></u>
<i><font color="#9A1900"> * Usage:</font></i>
<i><font color="#9A1900"> *</font></i>
<i><font color="#9A1900"> * adDelay.swapOut(arg1, arg2)</font></i>
<i><font color="#9A1900"> *     arg1 - the id of the placeholder div (in our example, skypePlaceholder)</font></i>
<i><font color="#9A1900"> *     arg2 - the id of the dummy div (in our example, skypeDummy) </font></i>
<i><font color="#9A1900"> *</font></i>
<i><font color="#9A1900"> */</font></i>
<b><font color="#0000FF">var</font></b> adDelay <font color="#990000">=</font> <font color="#FF0000">{</font>
  swapOut<font color="#990000">:</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>placeholderId<font color="#990000">,</font>dummyId<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#0000FF">var</font></b> dummy <font color="#990000">=</font> document<font color="#990000">.</font><b><font color="#000000">getElementById</font></b><font color="#990000">(</font>dummyId<font color="#990000">);</font>
    <b><font color="#0000FF">var</font></b> placeholder <font color="#990000">=</font> document<font color="#990000">.</font><b><font color="#000000">getElementById</font></b><font color="#990000">(</font>placeholderId<font color="#990000">);</font>
    <b><font color="#0000FF">if</font></b> <font color="#990000">(!</font>dummy<font color="#990000">)</font> <font color="#FF0000">{</font>
      <b><font color="#000000">alert</font></b><font color="#990000">(</font><font color="#FF0000">"element with id "</font> <font color="#990000">+</font> dummyId <font color="#990000">+</font> <font color="#FF0000">" does not exist"</font><font color="#990000">);</font>
    <font color="#FF0000">}</font>
    <b><font color="#0000FF">if</font></b> <font color="#990000">(!</font>placeholder<font color="#990000">)</font> <font color="#FF0000">{</font>
      <b><font color="#000000">alert</font></b><font color="#990000">(</font><font color="#FF0000">"element with id "</font> <font color="#990000">+</font> placeholderId <font color="#990000">+</font> <font color="#FF0000">" does not exist"</font><font color="#990000">);</font>
    <font color="#FF0000">}</font>

    <i><font color="#9A1900">/* loop through dummy nodes removing any JS nodes, preventing double execution */</font></i>
    <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>index <font color="#990000">=</font> dummy<font color="#990000">.</font>childNodes<font color="#990000">.</font>length <font color="#990000">-</font> <font color="#993399">1</font><font color="#990000">;</font> index <font color="#990000">&gt;=</font> <font color="#993399">0</font><font color="#990000">;</font> index<font color="#990000">--)</font>
    <font color="#FF0000">{</font>
      <b><font color="#0000FF">var</font></b> element <font color="#990000">=</font> dummy<font color="#990000">.</font>childNodes<font color="#990000">[</font>index<font color="#990000">];</font>
      <b><font color="#0000FF">if</font></b> <font color="#990000">((</font>element<font color="#990000">.</font>nodeName <font color="#990000">==</font> <font color="#FF0000">'SCRIPT'</font><font color="#990000">)</font> <font color="#990000">||</font> <font color="#990000">(</font>element<font color="#990000">.</font>nodeName <font color="#990000">==</font> <font color="#FF0000">'NOSCRIPT'</font><font color="#990000">))</font>
              dummy<font color="#990000">.</font><b><font color="#000000">removeChild</font></b><font color="#990000">(</font>element<font color="#990000">);</font>
    <font color="#FF0000">}</font>

    dummy<font color="#990000">.</font>parentNode<font color="#990000">.</font><b><font color="#000000">removeChild</font></b><font color="#990000">(</font>dummy<font color="#990000">);</font>
    placeholder<font color="#990000">.</font>parentNode<font color="#990000">.</font><b><font color="#000000">replaceChild</font></b><font color="#990000">(</font>dummy<font color="#990000">,</font> placeholder<font color="#990000">);</font>
    dummy<font color="#990000">.</font>style<font color="#990000">.</font>display <font color="#990000">=</font> <font color="#FF0000">'block'</font><font color="#990000">;</font>
  <font color="#FF0000">}</font>
<font color="#FF0000">}</font><font color="#990000">;</font>
</tt></pre>
<p></tt></pre>
<p>
After we've included the source to Ad Delay, we call adDelay.swapOut to perform the swapOut.  Your page now loads faster thanks to Ad Delay : = ).
</p>
<pre><tt><b><font color="#0000FF">&lt;script</font></b> <font color="#009900">type</font><font color="#990000">=</font><font color="#FF0000">"text/javascript"</font><b><font color="#0000FF">&gt;</font></b>
adDelay<font color="#990000">.</font><b><font color="#000000">swapOut</font></b><font color="#990000">(</font><font color="#FF0000">'skypeUsPlaceholder'</font><font color="#990000">,</font><font color="#FF0000">'skypeUsDummy'</font><font color="#990000">);</font>
<b><font color="#0000FF">&lt;/script&gt;</font></b>
</tt></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.getsnappy.com/blog/web-optimization-page-load-time/dont-let-ads-widgets-and-other-external-javascript-slow-you-down/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
