Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Unobtrusive JavaScript 137<br />
keep all JavaScript code, including event handlers, outside <strong>of</strong> the HTML. Stuart Langridge has since expanded upon<br />
this thought in book [10] and article format. [11]<br />
Separation <strong>of</strong> behavior from markup<br />
Traditionally, JavaScript <strong>of</strong>ten was placed inline together with an HTML document's markup. For example, the<br />
following is a typical implementation <strong>of</strong> JavaScript form validation when written inline:<br />
<br />
Adherents to "Unobtrusive <strong>Javascript</strong>" argue that the purpose <strong>of</strong> markup is to describe a document's structure, not its<br />
programmatic behavior and that combining the two negatively impacts a site's maintainability for similar reasons that<br />
combining content and presentation does. They also argue that inline event handlers are harder to use and maintain,<br />
when one needs to set several events on a single element or when one is using event delegation. Nor can they be used<br />
with custom events.<br />
The unobtrusive solution is to register the necessary event handlers programmatically, rather than inline. That is to<br />
say, rather than using the event handlers as shown with the hyperlink above to directly fire the desired action, the<br />
following methods are used. These are commonly achieved by assigning a particular CSS selector to all elements<br />
which need to be acted upon by the script:<br />
<br />
The script can then look for all input elements with the name date, and set them up accordingly:<br />
Using native JavaScript, HTML5 events and DOM Level 2 event listeners:<br />
document.addEventListener("domcontentloaded", function() {<br />
// domcontentloaded will fire when the DOM is loaded but unlike<br />
"load" it does not wait for images, etc.<br />
// It is being standardized in HTML5<br />
// It is assumed that there is only one element with name="date"<br />
document.getElementsByName("date")[0].addEventListener("change",<br />
validateDate, false);<br />
}, false);<br />
function validateDate(){<br />
// Do something when the content <strong>of</strong> the 'input' element with the<br />
name 'date' is changed.<br />
}<br />
The above example will not work in all browsers; some – particularly Internet Explorer (including version 8) – do<br />
not support DOM 2 event listeners. In practice developers <strong>of</strong>ten use libraries to get away from browser<br />
inconsistencies and deficiencies. The following script is specific to the MooTools JavaScript library, but<br />
accomplishes the same setup:<br />
window.addEvent('domready', function() {<br />
});<br />
$$('input[name=date]').addEvent('change', validateDate);<br />
The following script is specific to the jQuery JavaScript library:<br />
$(document).ready(function(){<br />
$('input[name=date]').bind('change', validateDate);