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:
“... imprisonment itself, entailing loss of liberty, loss of citizenship, separation from family and loved ones, is punishment enough for most individuals, no matter how favorable the circumstances under which the time is passed.”
—Mary B. Harris (18741957)
“There is a parallel between the twos and the tens. Tens are trying to test their abilities again, sizing up and experimenting to discover how to fit in. They dont mean everything they do and say. They are just testing. . . . Take a good deal of your daughters behavior with a grain of salt. Try to handle the really outrageous as matter-of-factly as you would a mistake in grammar or spelling.”
—Stella Chess (20th century)