Friday, May 22, 2009

What is Unobtrusive javascript ?

In simple words, it is separation of behaviour from the html structure.

Consider the following example :-



The style of this button element, to include the font of its caption, is provided by CSS rules loaded via a stylesheet. But while this declaration does not mix style with structure, it does mix behavior with structure, by including the JavaScript
that is to be executed when the button is clicked as part of the markup of the button element (which in this case, turns something named xyz red upon a click of the button).

For all the same reasons that it is desirable to segregate style and structure within an HTML document, it is also becoming recognized that separation of behavior from structure has just as many, if not more, benefits.

This movement is known as Unobtrusive Javascript.

jQuery is a javascript library which supports this movement. It aims to change the way that web developers fundamentally think about creating rich functionality in their pages. jQuery is generally useful for any page that needs to perform anything but the most trivial of JavaScript operations, but is also strongly focused on enabling page authors to employ the concept of Unobtrusive JavaScript within their pages. With this approach, behavior is separated from structure in the same way that CSS separates style from structure, achieving better page organization and increased code versatility.

No comments:

Post a Comment