<?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 paperCrane's view from the reverseFold &#187; onload</title>
	<atom:link href="http://www.reversefold.com/blog/tag/onload/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reversefold.com/blog</link>
	<description>Flex, PHP, and anything else I care to talk about</description>
	<lastBuildDate>Tue, 30 Aug 2011 08:51:28 +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>Running JavaScript when the page finishes loading</title>
		<link>http://www.reversefold.com/blog/2008/05/23/running-javascript-when-the-page-finishes-loading/</link>
		<comments>http://www.reversefold.com/blog/2008/05/23/running-javascript-when-the-page-finishes-loading/#comments</comments>
		<pubDate>Fri, 23 May 2008 21:54:29 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[onload]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/wordpress/?p=41</guid>
		<description><![CDATA[I know what you&#8217;re thinking. window.onload and &#60;body onload&#62; already let you run javascript when the page loads. Why do I need something more? The answer is that the standard onload only runs when the entire page and its dependencies are finished loading. This means that if you have multiple images (such as tracking tags) [...]]]></description>
			<content:encoded><![CDATA[<p>I know what you&#8217;re thinking. <tt>window.onload</tt> and <tt>&lt;body onload&gt;</tt> already let you run javascript when the page loads. Why do I need something more? The answer is that the standard <tt>onload</tt> only runs when the entire page <strong>and its dependencies</strong> are finished loading. This means that if you have multiple images (such as tracking tags) or scripts or a large swf, the <tt>onload</tt>ed code will only run once all of those pieces load. The point of the following is to run some JavaScript once the page itself has loaded and been parsed. This allows you to do things like set the background color, add additional elements, or start loading other dependencies while the rest of the page&#8217;s depdencies are loading. This is particularly useful if you need to do something on a page while a large swf is loading.</p>
<p>The following is a combination of <a href="http://dean.edwards.name/weblog/2005/09/busted/">several</a> <a href="http://dean.edwards.name/weblog/2006/06/again/">posts</a> on <a href="http://dean.edwards.name/weblog/">Dean Edwards&#8217; blog</a> as well as an <tt>onload</tt> handler for those browsers that don&#8217;t support this kind of solution that I found elsewhere. This may not work in Opera, I am not sure. According to the second post Opera supports <tt>DOMContentLoaded</tt> but I don&#8217;t know if this code will catch Opera properly.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// quit if this function has already been called</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arguments.<span style="color: #660066;">callee</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// flag this function so we don't do the same thing twice</span>
  arguments.<span style="color: #660066;">callee</span>.<span style="color: #660066;">done</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//CODE TO RUN GOES HERE</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// for Mozilla browsers</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DOMContentLoaded&quot;</span><span style="color: #339933;">,</span> init<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span>
<span style="color: #006600; font-style: italic;">// for Safari</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/WebKit/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">userAgent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// sniff</span>
  <span style="color: #003366; font-weight: bold;">var</span> _timer <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/loaded|complete/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      clearInterval<span style="color: #009900;">&#40;</span>_timer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// call the onload handler</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span>
<span style="color: #006600; font-style: italic;">//for those other poor souls</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">function</span> addLoadEvent<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> oldonload <span style="color: #339933;">=</span> window.<span style="color: #000066;">onload</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #000066;">onload</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        oldonload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        func<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  addLoadEvent<span style="color: #009900;">&#40;</span>init<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// for Internet Explorer (using conditional comments)</span>
<span style="color: #009966; font-style: italic;">/*@cc_on @*/</span>
<span style="color: #006600; font-style: italic;">/*@if (@_win32)
document.write(&quot;&lt;script id='__ie_onload' src='javascript:void(0)'&gt;&lt;/script&gt;&quot;);
var script = document.getElementById(&quot;__ie_onload&quot;);
script.onreadystatechange = function() {
  if (this.readyState == &quot;complete&quot;) {
    init(); // call the onload handler
  }
};
/*@end @*/</span></pre></div></div>

<p>UPDATE: fixed the code above. WordPress was screwing up the last bit due to its insistence on changing the preformatted HTML in the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2008/05/23/running-javascript-when-the-page-finishes-loading/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

