<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Enrique Delgado Monroy: Attaching functions to events in JavaScript: The unobstrusive way!</title>
    <link>http://www.enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Work, Life, Stuff</description>
    <item>
      <title>Attaching functions to events in JavaScript: The unobstrusive way!</title>
      <description>&lt;p&gt;I needed to set a simple focus when a page loads in a legacy site I was working on so I decided to exercise a &amp;#8220;better way&amp;#8221; to attach functions to events in JavaScript.&lt;/p&gt;


In the past, I used to attach &lt;code&gt;onLoad&lt;/code&gt; events through an anonymous function like so:
&lt;pre style="overflow: auto; background: #EEE;"&gt;
window.onload = function() {
  init();
}

function init() {
  //do something interesting
}
&lt;/pre&gt;
This method has its drawbacks tho; it assumes that this script is the only script that may act upon a document. We &amp;#8220;assume instead that it is part of a whole group of scripts all of which fulfilling different tasks.&amp;#8221; As described in this awesome &lt;a href="http://icant.co.uk/articles/from-dhtml-to-dom/from-dhtml-to-dom-scripting.html#domasset3"&gt;webzine&lt;/a&gt; by &lt;a href="http://icant.co.uk/articles/from-dhtml-to-dom/"&gt;Chris Heilmann&lt;/a&gt; .

Instead, Chris points out a better solution; something unobstrusive (because you don&amp;#8217;t have to mix JavaScript with &lt;span class="caps"&gt;HTML&lt;/span&gt;) and that plays well with others (because more than one script can act upon a document):
&lt;pre style="overflow: auto; background: #EEE;"&gt;
  // Attach the function init() to the window.onload event:
  addEvent(window,'load',init,false);

  // Initialization; I'm setting the focus on the first field:
  function init() {
    //do something interesting
  }

  // Function to attach events to objects.
  // (http://icant.co.uk/articles/from-dhtml-to-dom/from-dhtml-to-dom-scripting.html#domasset3)
  function addEvent(elm, evType, fn, useCapture){
    if (elm.addEventListener)
    {
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent) {
      var r = elm.attachEvent('on' + evType, fn);
      return r;
    } else {
      elm['on' + evType] = fn;
    }
  }
&lt;/pre&gt;

	&lt;p&gt;The key here is the way &lt;code&gt;addEvent&lt;/code&gt; adds event listeners to the document.&lt;/p&gt;</description>
      <pubDate>Thu, 03 May 2007 10:33:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:643e3e53-254e-4719-83d6-e3d3f97b236d</guid>
      <author>Enrique Delgado</author>
      <link>http://www.enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way</link>
      <category>Web Development</category>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>"Attaching functions to events in JavaScript: The unobstrusive way!" by Allie</title>
      <description>My bf is such a nerd... but i LOOOOOOVE him :)  </description>
      <pubDate>Fri, 24 Oct 2008 05:05:49 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:901fe213-5c4c-45b9-84d9-63f6aa691996</guid>
      <link>http://www.enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way#comment-217</link>
    </item>
    <item>
      <title>"Attaching functions to events in JavaScript: The unobstrusive way!" by Aidan Fraser</title>
      <description>Nice.</description>
      <pubDate>Mon, 20 Aug 2007 02:14:05 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ea471e89-0cae-407b-b270-f246113d44c9</guid>
      <link>http://www.enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way#comment-3</link>
    </item>
  </channel>
</rss>
