At the Forge - Unobtrusive JavaScript
Listing 5. atf-events-2.js, JavaScript Code for test-3.html
function set_event_handlers () {
function show_x_and_y(event) {
alert(event.pageX + ", " + event.pageY); return false;
}
$('hyperlink').observe('click', show_x_and_y);
$('hyperlink').observe('click', function() { alert('yay!'); return false;
});
$('hyperlink').onmouseover = function() { $('the_form').hide(); }
$('hyperlink').onmouseout = function() { $('the_form').show(); }
}
window.onload = set_event_handlers;
Our event handlers are now unobtrusive. However, there still are some problems associated with them. For example, what happens if we want to assign multiple handlers to a single event? That is, what if we want to execute not one function, but two, for $('hyperlink').onclick? In our current paradigm, we don't have any options; to have two functions execute, we need to wrap them both into a single function and then make that single wrapper function the event handlers.
This isn't much of a solution, particularly if we are loading third-party libraries that might want to attach handlers to one or more events. Instead, we need to use a different paradigm—one that lets us attach a handler to an event, rather than set the handler.
Prototype lets us do this with the observe method, which is available to any extended element—including those returned by the $() and $$() functions. So, we can say:
$('hyperlink').observe('click', show_x_and_y);
Because of the way that Prototype's observe method works, we can attach multiple handlers to a single event:
$('hyperlink').observe('click', show_x_and_y);
$('hyperlink').observe('click',
function() { alert('yay!'); return false;});
Of course, because this code still depends on the existence of $('hyperlink'), we still need to wrap it in a function that is then attached to window.onload. (We also can attach our function to the dom:loaded event, which fires before window.onload, but the idea is the same.)
An alternative solution is to use the Lowpro JavaScript library, which provides functions that facilitate easier writing of unobtrusive JavaScript.
By loading lowpro.js (after Prototype, but before any code that will use Lowpro), we gain access to the Event.addBehavior method, which lets us attach one or more events to any CSS selector. Listing 6 is a slight rewrite of our HTML file to include lowpro.js, and Listing 7 shows how we can set our event handlers using Event.addbehavior:
Event.addBehavior({
'#hyperlink:click' : show_x_and_y,
'#hyperlink:mouseover' : function() { $( 'the_form' ).hide() },
'#hyperlink:mouseout' : function() { $( 'the_form' ).show() }
});
We see that Event.addBehavior is a function that takes a single parameter, a JavaScript object (which we can think of as a hash). Each of the object's keys combines a CSS selector (#hyperlink in this case) with the name of an event, with a colon separating the two. Note that the event name does not include a leading “on”. So what would be the onmouseover handler is called mouseover for Event.addBehavior.
Listing 6. test-4.html, Using Lowpro
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Unobtrusive JavaScript</title>
</head>
<body>
<h1>Unobtrusive JavaScript</h1>
<script text="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/
↪prototype.js"></script>
<script text="text/javascript" src="lowpro.js"></script>
<script text="text/javascript" src="atf-events-3.js"></script>
<p>A paragraph of text.</p>
<p>A <a href="http://www.nytimes.com" id="hyperlink">hyperlink</a>
to The New York Times.</p>
<form method="POST" action="/action" id="the_form">
<input type="text" name="text_field" id="text_field" />
<input type="submit" value="Submit the form" id="submit_button" />
</form>
</body>
</html>
Listing 7. atf-events-3.js, Using Lowpro's Event-Adding Code
function show_x_and_y(event) {
alert(event.pageX + ", " + event.pageY); return false;
}
Event.addBehavior({
'#hyperlink:click' : show_x_and_y,
'#hyperlink:mouseover' : function() { $( 'the_form' ).hide() },
'#hyperlink:mouseout' : function() { $( 'the_form' ).show() }
});
As you can see in Listing 7, Event.addBehavior automatically wraps our event-handler definitions in code that waits for the entire page to load. So, we no longer need to set document.onload, for example.
Finally, the CSS selector code means we can set events on multiple elements simultaneously. If we want all paragraphs, or all table headers or even all images, we can do that quickly and easily with Lowpro. Lowpro allows us to reduce the amount of event-handling code that we write dramatically, keeping it in a single location and removing it from the HTML file where we might have first considered putting it.
I should add that Lowpro used to include DOM-manipulation routines as well, allowing us to add and modify page elements using a variety of convenience functions. However, recent versions of Prototype include this functionality already, allowing Lowpro to stick to behavior not addressed by Prototype.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Dart: a New Web Programming Experience
- Developer Poll
- What's the tweeting protocol?
- May 2013 Issue of Linux Journal: Raspberry Pi
- Reply to comment | Linux Journal
4 hours 3 min ago - Reply to comment | Linux Journal
4 hours 50 min ago - Web Hosting IQ
6 hours 24 min ago - Thanks for taking the time to
8 hours 1 min ago - Linux is good
9 hours 58 min ago - Reply to comment | Linux Journal
10 hours 16 min ago - Web Hosting IQ
10 hours 46 min ago - Web Hosting IQ
10 hours 46 min ago - Web Hosting IQ
10 hours 47 min ago - Reply to comment | Linux Journal
13 hours 47 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




Comments
So interresting...
Thanks for this article. Clear and so usefull...