Unobtrusive Java Script - Separation of Behavior From Markup

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:

    A separation situation is different for adults than it is for children. When we were very young children, a physical separation was interpreted as a violation of our inalienable rights....As we grew older, the withdrawal of love, whether that meant being misunderstood, mislabeled or slighted, became the separation situation we responded to.
    Roger Gould (20th century)

    To a first approximation, the intentional strategy consists of treating the object whose behavior you want to predict as a rational agent with beliefs and desires and other mental states exhibiting what Brentano and others call intentionality.
    Daniel Clement Dennett (b. 1942)