<?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>gHacks technology news &#187; delay javascript</title>
	<atom:link href="http://www.ghacks.net/tag/delay-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ghacks.net</link>
	<description>A technology blog covering software, mobile phones, gadgets, security, the Internet and other relevant areas.</description>
	<lastBuildDate>Tue, 24 Nov 2009 16:29:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Load your advertisements after your content</title>
		<link>http://www.ghacks.net/2009/04/06/load-your-advertisements-after-your-content/</link>
		<comments>http://www.ghacks.net/2009/04/06/load-your-advertisements-after-your-content/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 22:55:05 +0000</pubDate>
		<dc:creator>Daniel Pataki</dc:creator>
				<category><![CDATA[Revenue Sources]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[advertising]]></category>
		<category><![CDATA[delay javascript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[loading time]]></category>

		<guid isPermaLink="false">http://www.ghacks.net/?p=11700</guid>
		<description><![CDATA[I didn&#8217;t have much use for loading ads after my content, since on most of my sites and those I worked on, they were near the end of the code anyway, but while working on a site today I ran into a problem. There is an ad at the top of the design which tends [...]]]></description>
			<content:encoded><![CDATA[<p>I didn&#8217;t have much use for loading ads after my content, since on most of my sites and those I worked on, they were near the end of the code anyway, but while working on a site today I ran into a problem. There is an ad at the top of the design which tends to load slowly. I have specific requirements that the site should be as fast as possible, so I had to devise a way of loading the ad last. I went through some methods I found online after things off the top of my head didn&#8217;t work, but none helped, so I came up with my own code for it.</p>
<p>I am not taking credit for this, so if you did this before me feel free to let us know, but I did arrive at it on my own. The methods you could try is the &#8220;defer&#8221; attribute, which defers loading to the end of the page load, however, this had no effect at all. You can also try a &#8220;window.onload&#8221;, but this didn&#8217;t help either. With any other function combinations the ad either loaded as usual or did some weird stuff.</p>
<p>In the end, I decided to load the ad at the bottom of the page, right before the &#8220;&#8221; tag, and then use javascript to &#8220;transfer&#8221; the contents to where I need it do be, while keeping the source hidden. This is achieved using the &#8220;document.getElementById&#8221; method, read on to take a look at a specific example.</p>
<p><span id="more-11700"></span></p>
<p>So what we need to do is create a div at the bottom of our page, load the advertisement there, and transfer it to the place we want it to be. I will be using inline CSS and inline javascript, which should not be done in real &#8220;life&#8221;, the best practice is to have all your CSS in external stylsheets and you javascript called from files in the header (where possible). The bottom of our page would look like this:</p>
<p>&lt;div id=&#8221;top_ad_loader&#8221; style=&#8221;display:none;&#8221;&gt;<br />
Ad code in here<br />
&lt;/div&gt;</p>
<p>&lt;script type=&#8221;text/javascript&#8221;&gt;<br />
document.getElementById(&#8221;top_ad&#8221;).innerHTML = document.getElementById(&#8221;top_ad_loader&#8221;).innerHTML<br />
&lt;/script&gt;</p>
<p>&lt;/body&gt;</p>
<p>So what is going on here? We have two divs, the first is &#8220;top_ad_loader&#8221;, which you can see right here. This div is hidden, but contains the ad code. When the code is being read, the javascript might take a while to load, but we&#8217;re right at the bottom, so all our content is displayed already. There is also another div, &#8220;top_ad&#8221;, which can not be seen here, it is somewhere way above this part of our code, somewhere near the &#8220;&lt;body&gt;&#8221; tag.</p>
<p>There is javascript code right beneath the ad loader, the purpose of which is to transfer the loaded contents of the ad loader to te place where we want it to be. We &#8220;grab&#8221; the contents inside the ad loader using &#8220;<strong>document.getElementById(&#8221;top_ad_loader&#8221;).innerHTML</strong>&#8220;, and we want the contents of the actual ad block to equal this.</p>
<p>Once the page load gets to the ad it will slowly load it, when finished, parsing will continue, and our javascript will transfer the contents to the top.</p>
<p>If you have a page that loads a bit slowly, perhaps this method would be worth a try? Contents usually load faster than javascript, so if you place the javascript load last your content will load in 1-2 seconds (maybe much less), making the javascript load 1-2 seconds later. However, if a javascript at the beginning loads in 5 seconds, you need to wait that out just to start loading the content.</p>
<p><img class="alignleft size-full wp-image-10878" src="http://www.ghacks.net/wp-content/uploads/2009/03/scrip_twitter.gif" alt="Script" width="53" height="53" /><strong>If you&#8217;d like to read some similar articles, take a look at <a title="Web development blog" href="http://scriptastique.com">Scriptastique</a>, a blog all about web development and coding, with great tips on CSS, HTML, PHP, MySQL and Javascript and tutorials and screencasts coming soon! You can follow us on our <a title="Scriptastique RSS feed" href="http://feeds2.feedburner.com/scriptastique">RSS feed</a>, or <a title="Scriptastique on Twitter" href="http://twitter.com/scriptastique">Twitter</a> where we&#8217;re posting 3-4 short tips daily now!</strong></p>

	Tags: <a href="http://www.ghacks.net/tag/advertising/" title="advertising" rel="tag">advertising</a>, <a href="http://www.ghacks.net/tag/delay-javascript/" title="delay javascript" rel="tag">delay javascript</a>, <a href="http://www.ghacks.net/tag/javascript/" title="javascript" rel="tag">javascript</a>, <a href="http://www.ghacks.net/tag/loading-time/" title="loading time" rel="tag">loading time</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.ghacks.net/2008/06/27/you-better-stop-using-internet-explorer-for-now/" title="You better stop using Internet Explorer for now (June 27, 2008)">You better stop using Internet Explorer for now</a> (18)</li>
	<li><a href="http://www.ghacks.net/2007/08/08/why-i-disabled-google-analytics-and-wp-cache/" title="Why I disabled Google Analytics and WP-Cache (August 8, 2007)">Why I disabled Google Analytics and WP-Cache</a> (4)</li>
	<li><a href="http://www.ghacks.net/2008/08/19/view-javascript-sources-with-jsview/" title="View Javascript Sources with JSView (August 19, 2008)">View Javascript Sources with JSView</a> (1)</li>
	<li><a href="http://www.ghacks.net/2008/10/17/test-your-browsers-javascript-performance/" title="Test Your Browser&#8217;s JavaScript Performance (October 17, 2008)">Test Your Browser&#8217;s JavaScript Performance</a> (5)</li>
	<li><a href="http://www.ghacks.net/2009/08/16/siteflow-bookmarklet-simplifies-website-navigation/" title="SiteFlow Bookmarklet Simplifies Website Navigation (August 16, 2009)">SiteFlow Bookmarklet Simplifies Website Navigation</a> (3)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ghacks.net/2009/04/06/load-your-advertisements-after-your-content/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
