<?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>Talkwards &#187; JavaScript</title>
	<atom:link href="http://www.talkwards.com/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.talkwards.com</link>
	<description>Advancing Talkwards...</description>
	<lastBuildDate>Sat, 03 Sep 2011 16:33:28 +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>How to add a body on load function with Javascript</title>
		<link>http://www.talkwards.com/2007/07/how-to-add-a-body-on-load-function-with-javascript</link>
		<comments>http://www.talkwards.com/2007/07/how-to-add-a-body-on-load-function-with-javascript#comments</comments>
		<pubDate>Sun, 29 Jul 2007 02:21:34 +0000</pubDate>
		<dc:creator>Hoakz</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.hoakz.com/blog/?p=97</guid>
		<description><![CDATA[This is an article on how to add a javascript function that will be run when a webpage has loaded. We begin by defining a function for running after a page (or actually window) has been loaded: function bodyOnLoad() { .. .. } And then we&#8217;ll do: window.onload = bodyOnLoad; However, we also want to [...]]]></description>
			<content:encoded><![CDATA[<p>This is an article on how to add a javascript function that will be run when a webpage has loaded.  We begin by defining a function for running after a page (or actually window) has been loaded:</p>
<pre style="padding-left: 30px;">function bodyOnLoad() {
  ..
  ..
}</pre>
<p>And then we&#8217;ll do:</p>
<pre style="padding-left: 30px;">window.onload = bodyOnLoad;</pre>
<p>However, we also want to make sure our setting of the load event doesn&#8217;t remove some other setting. This is done by also keeping any older events.</p>
<p>We store the previous on load even by doing;</p>
<pre style="padding-left: 30px;">var prevOnLoad = window.onload;</pre>
<p>And we redefine our bodyOnLoad function:</p>
<pre style="padding-left: 30px;">function bodyOnLoad() {
  prevOnLoad();
  ..
  ..
}</pre>
<p>However we can make the creation of the function and the setting of the event a little bit more effective by doing:</p>
<pre style="padding-left: 30px;">window.onload = function() {
  prevOnLoad();

  ..
  ..
}</pre>
<p><em>You still need to get prevonload before you do that</em></p>
<p>This becomes even more obvious once we create a function for adding new load events:</p>
<pre style="padding-left: 30px;">function addLoadEvent(func) {
  var prevOnLoad = window.onload;
  window.onload = function() {
    prevOnLoad();
    func();
  }
}</pre>
<p>In this way we can concentrate on creating the new load event outside of the function for adding it to the window.onload.</p>
<pre style="padding-left: 30px;">function myEvent(){
  ..
  ..
}
addLoadEvent(myEvent);</pre>
<p>We might even do:</p>
<pre style="padding-left: 30px;">addLoadEvent(
  function (){
    ..
    ..
  }
);</pre>
<p><em>Notice the difference between curly braces &#8220;{}&#8221; and parenthesis &#8220;()&#8221;</em></p>
<p>Finally, we have to make sure there is a load event set for the window before calling it from the new event, so we need to check for this:</p>
<pre style="padding-left: 30px;">function addLoadEvent(func) {
  var prevOnLoad = window.onload;
  if (typeof prevOnLoad != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      prevOnLoad();
      func();
    }
  }
}</pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.talkwards.com%2F2007%2F07%2Fhow-to-add-a-body-on-load-function-with-javascript&amp;title=How%20to%20add%20a%20body%20on%20load%20function%20with%20Javascript" id="wpa2a_2"><img src="http://www.talkwards.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.talkwards.com/2007/07/how-to-add-a-body-on-load-function-with-javascript/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

