<?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>Jeremy Fry</title>
	<atom:link href="http://www.jeremy-fry.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremy-fry.com</link>
	<description>Sir Web Developer</description>
	<lastBuildDate>Sat, 04 Feb 2012 04:10:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Waiting for the DOM</title>
		<link>http://www.jeremy-fry.com/when-to-wait/</link>
		<comments>http://www.jeremy-fry.com/when-to-wait/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 14:55:35 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://dev.jeremy-fry.com/?p=60</guid>
		<description><![CDATA[Should I place my Javascript in the footer or in the header is a question I&#8217;ve seen come under fire a lot lately. There are numerous blogs touting the advantages of moving all your Javascript to the footer. I haven&#8217;t &#8230; <a href="http://www.jeremy-fry.com/when-to-wait/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Should I place my Javascript in the footer or in the header is a question I&#8217;ve seen come under fire a lot lately. There are numerous blogs touting the advantages of moving <strong>all</strong> your Javascript to the footer. I haven&#8217;t seen any articles on why moving your Javascript to the footer, or wrapping all jQuery in a document ready is a bad call.</p>
<p>Moving your javascript down the page does have a lot of advantages. To use an common phrase: &#8220;Content is King.&#8221; Generally speaking the first thing a user wants when they go to your site is content. By pushing scripts to the bottom of the page you free up the browser retrieve and display content first. But there are some serious questions you should ask yourself before you move all your scripts to the footer.</p>
<p><strong>Does this Javascript have a profound impact on the users experience?</strong> Does your script dramatically change the way content is displayed or interacted with on your page? If it does, it is worth weighing the what is more important to your visitor. Take a look at <a title="Woot.com" href="http://woot.com" target="_blank">Woot.com</a> for example. They sell 1 product a day, so obviously the most important thing to their visitors is seeing what that one product is. If you click on the image as soon as it&#8217;s visible you get taken to the full size image on a new page. But what if you wait for the page to load? Now your presented with a slideshow of images showing the product from every angle.</p>
<p>This is a prime example of when not to move your scripts to the footer. When I visit Woot.com the first thing I do is click on this product image. If I click too early I&#8217;m punished for it and have to click back on my browser and wait patiently for the page to finish loading. Depending on what time you view Woot.com has a pretty dramatic effect on load times though. Which raises another question.</p>
<p><strong>Does my ad network have an effect on my scripts? </strong> It&#8217;s not that the whole page is loading terribly slow, it&#8217;s that sometimes the ad network is performing painfully slow. This means I get to wait for the ad network to respond before I can use the page the way it was meant to be used. Keep in mind that some ad networks load asynchronously and will not cause your page to hang, but there&#8217;s a lot of them that don&#8217;t. Be mindful of how your ad network delivers ads and adjust your scripts to give a better user experience.</p>
<p><strong>Should I user $(document).ready with my jQuery? </strong>Now that frameworks like jQuery and YUI are becoming excedingly common, it&#8217;s worth looking at how they are effect by script placement. The $(document).ready call with jQuery waits for the entire DOM to be present before running any scripts (this is different than waiting for the page to load). Let take another look at <a title="Woot.com" href="http://woot.com" target="_blank">Woot.com</a>. Their account/login buttons are shown by Javascript.</p>
<p>What I find interesting though, is that which div to display (login or account) is delayed until the DOM is loaded. The account button shown there is the only way to navigate to your account page, but once again you have to wait for the ad network to deliver ads before the rest of the DOM is loaded. If things are moving slowly that day, you have to wait up to 5 seconds to see something that is loaded and ready, but hidden from you!</p>
<p><strong>How can you fix this issues? </strong>Be careful about what you put in the footer and what you put in a DOM loaded function. If you have something user critical, don&#8217;t be afraid to toss in a script tag just after that element with whatever Javascript is needed to make it function correct.</p>
<p>Take advantage of <a title="Google Libraries" href="http://code.google.com/apis/libraries/" target="_blank">Google Libraries API</a>. Using a Google hosted version of jQuery (or other framework) gives the best chance that your visitor has a cached version, saving load times while keeping the script in the header. Also look at options of compressing or minifying your Javascript so it can live in your header but with a reduced file size.</p>
<p>Don&#8217;t use Javascript for critical functions on your site. Yeah I know it&#8217;s 2011 and Javascript is no longer thought of as an evil thing, but there are still people who chose to browse with Javascript disabled. Keep this in mind when developing and as yourself if you could achieve the same effect with CSS or some server side script.</p>
<p>There really isn&#8217;t a do this, or don&#8217;t do this answer in the end. You have to use your better judgement and remember that just because someone says you should do it, doesn&#8217;t mean it right. Always step back and try to look at thing from the visitors perspective. What am I here for? What do I want to do or see first? In the end you always want your average visitor to be happy, not the guy checking your source and looking for where you included your Javascript.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremy-fry.com/when-to-wait/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

