<?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: Tag javascript</title>
    <link>http://www.enriquedelgado.com/articles/tag/javascript</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Work, Life, Stuff</description>
    <item>
      <title>Attaching functions to events in JavaScript: The unobstrusive way! - Part 2</title>
      <description>&lt;p&gt;&lt;a href="http://www.enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way"&gt;Previously&lt;/a&gt;, I talked about a good way to attach event listeners to &lt;span class="caps"&gt;DOM&lt;/span&gt; elements. I like the approach described in that post because is pure JavaScript and not dependent on a framework, but if you happen to be using a framework already, or are thinking on using some, here are two methods of achieving this task:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://prototypejs.org/api/event"&gt;Prototype.js&lt;/a&gt;: &lt;tt&gt;observe&lt;/tt&gt; method.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;: &lt;tt&gt;bind&lt;/tt&gt; method and friends.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;It is important to remember that, your code should wait for the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree to completely load before attempting to attach events to elements. The typical scenario is that some JavaScript code is attempting to attach an event to an element that does not exist yet, because the document is still loading.&lt;/p&gt;


	&lt;p&gt;Each framework has ways of wrapping your code, so that it runs only when the &lt;span class="caps"&gt;DOM&lt;/span&gt; is ready:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;Prototype&lt;/strong&gt;&lt;br/&gt;&lt;pre style="overflow: auto; background: #EEE;"&gt;
document.observe("dom:loaded", function() {
  // Your code here
});
&lt;/pre&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;jQuery&lt;/strong&gt;&lt;br/&gt;&lt;pre style="overflow: auto; background: #EEE;"&gt;
$(document).ready(function(){
   // Your code here
 });

&lt;/pre&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;Using Prototype&lt;/h3&gt;


	&lt;p&gt;Prototype offers several event handling methods, but the most common is &lt;code&gt;observe&lt;/code&gt; and &lt;code&gt;stopObserving&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Example:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$('foo').observe('click', respondToClick);

function respondToClick(event) {
  // This is how you access the element that triggered the event:
  var element = event.element();
  // Your code here, e.g.:
  alert('Hello');
}
&lt;/pre&gt;

	&lt;p&gt;Notice the name of the event, in this case is &lt;tt&gt;click&lt;/tt&gt;. The complete list of events are defined in the &lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings"&gt;&lt;span class="caps"&gt;W3C&lt;/span&gt; recommendations&lt;/a&gt;, but here is a summary of the most common event names organized by type:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Mouse Events
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;click&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is clicked over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mousedown&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is pressed over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseup&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is released over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseover&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved onto an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mousemove&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved while it is over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseout&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved away from an element.&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
		&lt;li&gt;Key Events
	&lt;ul&gt;
	&lt;li&gt;&lt;span class="caps"&gt;W3C&lt;/span&gt; does not have them, but the following are supplied by Prototype:&lt;br /&gt;&lt;tt&gt;&lt;span class="caps"&gt;KEY&lt;/span&gt;_BACKSPACE, &lt;span class="caps"&gt;KEY&lt;/span&gt;_TAB, &lt;span class="caps"&gt;KEY&lt;/span&gt;_RETURN, &lt;span class="caps"&gt;KEY&lt;/span&gt;_ESC, &lt;span class="caps"&gt;KEY&lt;/span&gt;_LEFT, &lt;span class="caps"&gt;KEY&lt;/span&gt;_UP, &lt;span class="caps"&gt;KEY&lt;/span&gt;_RIGHT, &lt;span class="caps"&gt;KEY&lt;/span&gt;_DOWN, &lt;span class="caps"&gt;KEY&lt;/span&gt;_DELETE, &lt;span class="caps"&gt;KEY&lt;/span&gt;_HOME, &lt;span class="caps"&gt;KEY&lt;/span&gt;_END, &lt;span class="caps"&gt;KEY&lt;/span&gt;_PAGEUP, &lt;span class="caps"&gt;KEY&lt;/span&gt;_PAGEDOWN&lt;/tt&gt;&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;HTML&lt;/span&gt; Events
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;load&lt;/tt&gt; &amp;#8211; Occurs when the &lt;span class="caps"&gt;DOM&lt;/span&gt; implementation finishes loading.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;unload&lt;/tt&gt; &amp;#8211; Occurs when the &lt;span class="caps"&gt;DOM&lt;/span&gt; implementation removes a document from a window or frame.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;select&lt;/tt&gt; &amp;#8211; Occurs when a user selects some text in a text field.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;change&lt;/tt&gt; &amp;#8211; Occurs when a control loses the input focus and its value has been modified since gaining focus.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;submit&lt;/tt&gt; &amp;#8211; Occurs when a form is submitted.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;focus&lt;/tt&gt; &amp;#8211; Occurs when an element receives focus either via a pointing device or by tabbing navigation.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;blur&lt;/tt&gt; &amp;#8211; Occurs when an element loses focus either via the pointing device or by tabbing navigation.&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;Using jQuery&lt;/h3&gt;


	&lt;p&gt;jQuery mostly has &amp;#8220;helper methods&amp;#8221; to set event listeners that are named after the event name, but the two most basic methods are &lt;tt&gt;bind&lt;/tt&gt; and &lt;tt&gt;unbind&lt;/tt&gt;.&lt;/p&gt;


	&lt;p&gt;Example:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$("#foo").bind("click", function(e){
  // This is how you access the element that triggered the event:
  var element = event.target;
  // Your code here, e.g.:
  alert('Hello');
});
&lt;/pre&gt;

Again, notice the name of the event, in this case is &lt;tt&gt;click&lt;/tt&gt;. According to the jQuery documentation, these are the allowed event names:
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;blur, focus, load, resize, scroll, unload, click, dblclick,  mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select,  submit, keydown, keypress, keyup, error&lt;/tt&gt;.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Most helper elements are named after the event names above, so you have things like &lt;tt&gt;click()&lt;/tt&gt;, &lt;tt&gt;focus()&lt;/tt&gt;, &lt;tt&gt;submit()&lt;/tt&gt;, etc.&lt;/p&gt;


	&lt;p&gt;Example using an anonymous function as the functioned to be executed when the event is triggered:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$("#foo").click(function () { 
  // The element is access by using "$(this)" 
  // Your code here, e.g.:
  alert('Hello');
});
&lt;/pre&gt;

	&lt;h3&gt;Event Delegation&lt;/h3&gt;


	&lt;p&gt;One often source of confusion is how to attach event listeners to dynamically-generated &lt;span class="caps"&gt;DOM&lt;/span&gt; elements (for example, adding &lt;tt&gt;&amp;lt;li&amp;gt;&lt;/tt&gt; elements to a list via &lt;span class="caps"&gt;AJAX&lt;/span&gt;).&lt;/p&gt;


	&lt;p&gt;When an element is crated, it is not already bound to an event listener, so you will have to perform a two-step process: create the element in the &lt;span class="caps"&gt;DOM&lt;/span&gt;, and then attach the event listener. This can get tedious easily, so instead of attaching the event listener to the &lt;span class="caps"&gt;DOM&lt;/span&gt; element desired, you attach an event listener to a parent element in the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree.&lt;/p&gt;


	&lt;p&gt;In turn, this parent element acts as a &amp;#8220;catch all&amp;#8221; event listener. It will figure out which element triggered the event in the first place and act accordingly. This technique is referred to as &lt;a href="http://icant.co.uk/sandbox/eventdelegation/"&gt;event delegation&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Event delegation works because some events &amp;#8220;bubble up&amp;#8221; from child nodes to parent nodes in the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree; see &lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling"&gt;event bubbling&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Hope thins helps someone out there getting started. Post your questions if you run into trouble. :)&lt;/p&gt;</description>
      <pubDate>Fri, 24 Oct 2008 08:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3690a8d5-8f49-4316-95b8-250cc37c4f3f</guid>
      <author>Enrique Delgado</author>
      <link>http://www.enriquedelgado.com/articles/2008/10/24/attaching-functions-to-events-in-javascript-the-unobstrusive-way-part-2</link>
      <category>Web Development</category>
      <category>javascript</category>
      <category>html</category>
      <category>DOM</category>
      <category>prototype</category>
      <category>jquery</category>
    </item>
    <item>
      <title>Enhance your JavaScript-fu with LowPro</title>
      <description>&lt;p&gt;During RailsConf 2007 I had the pleasure to have a beer with &lt;a href="http://danwebb.net"&gt;Dan Webb&lt;/a&gt; at the &lt;a href="http://www.railsmachine.com/"&gt;Rails Machine&lt;/a&gt; hangout. It was cool to talk all about JavaScript (one of my interests) as it relates to Rails.&lt;/p&gt;


	&lt;p&gt;It was interesting to find out that Rails is not quite adopting the &amp;#8220;unobtrusive way&amp;#8221; completely. Just look at how the &lt;code&gt;link_to_remote&lt;/code&gt; JavaScript code is generated; it mixes content with JS code.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m hoping that as Rails gets betetr and better, it will also support better graceful degradation for JavaScript. For now, Dan&amp;#8217;s awesome library a.k.a &lt;a href="http://www.danwebb.net/2006/9/3/low-pro-unobtrusive-scripting-for-prototype"&gt;Low Pro&lt;/a&gt;  should help us all become true JavaScript-fu disciples.&lt;/p&gt;


	&lt;p&gt;Talk about navigating through the &lt;span class="caps"&gt;DOM&lt;/span&gt; with ninja-like swiftness! :)&lt;/p&gt;</description>
      <pubDate>Thu, 31 May 2007 09:34:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:660b9d9f-ae2a-4848-9d9c-f7a457a3e9ef</guid>
      <author>Enrique Delgado</author>
      <link>http://www.enriquedelgado.com/articles/2007/05/31/enhance-your-javascript-fu-with-lowpro</link>
      <category>Web Development</category>
      <category>javascript</category>
      <category>rails</category>
    </item>
    <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>
  </channel>
</rss>
