Explains the notion of Unobtrusive JavaScript, Layering, and Separation of Concerns.
It is considered a best practice to separate web pages into three areas of concern: content (HTML), style (CSS), and behavior (JavaScript). It makes these pages better organized, more understandable, and easier-to-maintain.
The term Unobtrusive JavaScript refers to how you use JavaScript on web pages. The unobtrusive part denotes out-of-the-way, discreet, and without interference. Initially it meant that if a browser did not support certain features or if JavaScript had been disabled the user would not be shut out. If possible, JavaScript would run, but if not then the site would continue to present the necessary information. This approach is also referred to as progressive enhancement.
More recently however, the role of JavaScript has become so critical that many new sites simply cannot function without it and therefore most developers pretty much ignore the possibility of JavaScript not being available. For example, single page applications (SPAs) that rely on Ajax to perform partial page refreshes will simply not work without JavaScript.
However, the concept of unobtrusive JavaScript lives on as a way of structuring web pages; this time from the perspective of the developer and how they layer (i.e. separate) their code. The idea is that JavaScript should not be embedded with HTML, but rather there needs to be a separation of behavior (JavaScript) and content (HTML).
In many web pages you see JavaScript assigned to an onclick attribute on a DOM element, like so:
<button onclick="alert('Hello');return false;" >Click me</button>
This works well on pages with just a few JavaScript snippets, but when your application starts to grow and you have hundreds of DOM elements across many pages that require, say, JavaScript client-side validation, then it becomes very difficult to maintain. This is why it is best to separate the JavaScript from the HTML.
The notion of separating web pages in layers has another dimension: styles (using CSS). These three dimensions: behavior, style, and content are frequently referred to as the three layers of web design. Here is a graphic of these layers.
Notice in the figure that each layer has its own file type: HTML resides in .html files (or something else depending on the technology used), JavaScript in .js files, and Style Sheets in .css files. This greatly facilitates a clean separation of concerns.
Here is an example of the three layers:
HTML<div class="area"> <button id="clicker">Click here</button> </div>JavaScript
$(function() { $("#clicker").on('click', function() { alert("Yep, clicked!"); }); }CSS
.area { padding:10px; background:lightblue; }
The benefits of organizing your pages in separate layers are numerous:
Concerning the last point, this is less and less an issue today. Browser makers are beginning to push users to upgrade more quickly to more recent browser versions. All browsers today accept HTML, CSS, and JavaScript. In fact, we are on the cusp of the next stage: HTML5 and CSS3. Right now, we are beginning to see developers writing HTML5 and CSS3 web apps knowing that, for the time being, they will miss out on customers that do not keep up with their browser versions. Hopefully, their content and presentation is so compelling that it will spur a worldwide flurry of browser upgrades which would make any web developers very happy.
As an aside, when using an MV* framework (MVC, MVP, MVVM) you typically also use a templating engine (such as mustache.js, underscore.js, or the built-in jQuery templating engine). In those cases the data is dynamically injected into the HTML and maintained by data binding. This is a very powerful concept and another step in facilitating the aforementioned separation of concerns. MV* frameworks are discussed in the Model View Pattern section. Templating is demonstrated in the 'Patterns in Action' section.