Separation of Behavior From Markup
Traditionally, JavaScript was often placed inline together with an HTML document's markup. For example, the following is a typical implementation of JavaScript form validation when written inline:
Adherents to "Unobtrusive Javascript" argue that the purpose of markup is to describe a document's structure, not its programmatic behavior and that combining the two negatively impacts a site's maintainability for similar reasons that combining content and presentation does. They also argue that inline event handlers are harder to use and maintain, when one needs to set handlers for several events on a single element, when one wants to set the same event handler on several elements, or when one is using event delegation. Nor can they be used with custom events.
The unobtrusive solution is to register the necessary event handlers programmatically, rather than inline. Rather than adding the onchange attribute explicitly as above, the relevant element(s) are simply identified, for example by class, id or some other means in the markup:
A script that runs when the page is first loaded into the browser can then look for the relevant element(s) and set them up accordingly:
window.onload = function { document.getElementById('date').onchange = validateDate; };Read more about this topic: Unobtrusive Java Script
Famous quotes containing the words separation and/or behavior:
“On a subconscious level your child experiences separation from you as a punishment. And if he is to be rewarded with your return, he must be very good.... Eager for you to come back, your finicky son would probably eat liver if your baby-sitter served it, and he wouldnt dream of resisting her at bathtime.”
—Cathy Rindner Tempelsman (20th century)
“The civilizing process has increased the distance between behavior and the impulse life of the animal body.”
—Shoshana Zuboff (b. 1951)