Unobtrusive JavaScript
   HOME

TheInfoList



OR:

Unobtrusive JavaScript is a general approach to the use of
client-side Client-side refers to operations that are performed by the client in a client–server relationship in a computer network. General concepts Typically, a client is a computer application, such as a web browser, that runs on a user's local comput ...
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
in web pages so that if JavaScript features are partially or fully absent in a user's
web browser A web browser is application software for accessing websites. When a user requests a web page from a particular website, the browser retrieves its files from a web server and then displays the page on the user's screen. Browsers are used o ...
, then the user notices as little as possible any lack of the web page's JavaScript functionality. The term has been used by different technical writers to emphasize different aspects of front-end web development. For some writers, the term has been understood more generally to refer to separation of functionality (the "behavior layer") from a web page's structure/content and
presentation A presentation conveys information from a speaker to an audience. Presentations are typically demonstrations, introduction, lecture, or speech meant to inform, persuade, inspire, motivate, build goodwill, or present a new idea/product. Presenta ...
, while other writers have used the term more precisely to refer to the use of
progressive enhancement Progressive enhancement is a strategy in web design that puts emphasis on web content first, allowing everyone to access the basic content and functionality of a web page, whilst users with additional browser features or faster Internet access ...
to support user agents that lack certain JavaScript functionality and users that have disabled JavaScript. Following the latter definition, unobtrusive JavaScript contributes to
web accessibility Web accessibility, or eAccessibility,European CommissionCommunication from the Commission to the Council, the European Parliament and the , European Economic and Social Committee and the Committee of the Regions: eAccessibility, EC(2005)1095 pub ...
insofar as it helps ensure that all users—whatever their computing platform—get roughly equal access to all of the web page's information and functionality.


Overview

A typical client-side dynamic web page can be conceived as consisting of four parts: the marked-up content (
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaSc ...
), the style sheet ( CSS),
client-side JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, often ...
, and embedded objects such as images. The client-side JavaScript part can be conceived as enhancing the other parts by adding features or functionality that would not be possible without JavaScript. The concept of "unobtrusiveness" in relation to client-side JavaScript was coined in 2002 by Stuart Langridge in the article "Unobtrusive DHTML, and the power of unordered lists". In the article Langridge argued for a way to keep all JavaScript code, including event handlers, outside of the HTML when using
dynamic HTML Dynamic HTML, or DHTML, is a term which was used by some browser vendors to describe the combination of HTML, style sheets and client-side scripts (JavaScript, VBScript, or any other supported scripts) that enabled the creation of interactive ...
(DHTML). He said that the purpose of this kind of organization of code was "providing a better user experience for people whose browsers can support it, and those whose browsers cannot", while also making scripting easier for web developers. Langridge later expanded upon this thought and emphasized that the core meaning of "unobtrusive" is that "if a given Web browser doesn't support the DHTML features you're using, that absence should affect the user experience as little as possible". (Reference to the first edition, since it shows how the author pioneered the concept.) The same passage is in an article excerpted from the book: In other words, for Langridge, "unobtrusive" principally refers to users' experience of the absence of JavaScript features in a given situation.


Variant definitions

Other authors have described variations on the essential elements of unobtrusiveness. David Flanagan's book ''JavaScript: The Definitive Guide'' (2006) said that while there is no specific formula, there are three main goals of unobtrusive JavaScript: * To separate JavaScript from HTML markup, as well as keeping modules of JavaScript independent of other modules using basic conventions such as the use of
namespace In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified. Namespaces ...
s to prevent namespace collisions and the use of module initialization code; * To degrade gracefully—all content should be available without all or any of the JavaScript running successfully; * To not impede the accessibility of the HTML, and ideally to improve it, whether the user has personal disabilities or are using an unusual, or unusually configured, browser. The Web Standards Project, in its ''JavaScript Manifesto'' (2006), said that the "purpose of JavaScript is enhancing the usability of web pages by adding interaction to them", and described four benefits of unobtrusive DOM scripting: # Usability: An unobtrusive DOM script does not draw the attention of the user—"visitors just use it without thinking about it." #
Graceful degradation Fault tolerance is the property that enables a system to continue operating properly in the event of the failure of one or more faults within some of its components. If its operating quality decreases at all, the decrease is proportional to the ...
: Unobtrusive DOM scripts never generate error messages, in any browser, even when they fail. If features cannot be presented properly, they silently disappear. # Accessibility: If any script fails, the page still delivers its core functions and information via the markup, stylesheets and/or server-side scripting. # Separation: For the benefit of other and future web developers, all JavaScript code is maintained separately, without impacting other files of script, markup or code. For the Paris
Web Conference Web conferencing is used as an umbrella term for various types of online conferencing and collaborative services including webinars (web seminars), webcasts, and web meetings. Sometimes it may be used also in the more narrow sense of the peer-l ...
in 2007, Christian Heilmann identified seven rules of unobtrusive JavaScript, some of which were wider in scope than other narrower definitions of "unobtrusive": See also: # "Do not make any assumptions":
Defensive programming Defensive programming is a form of defensive design intended to develop programs that are capable of detecting potential security abnormalities and make predetermined responses. It ensures the continuing function of a piece of software under un ...
techniques should allow for the possibilities that JavaScript may not run, the browser may not support expected methods, the HTML may have changed, unexpected input devices may be in use and other scripts may either not be present or may be encroaching on the global namespace. # "Find your hooks and relationships", such as IDs and other aspects of the expected HTML. # Leave traversing individual DOM objects to the experts, such as to the CSS handler built into the browser where possible. # "Understand browsers and users", particularly how browsers fail, what assumptions users make, and unusual configurations or usages. # "Understand
events Event may refer to: Gatherings of people * Ceremony, an event of ritual significance, performed on a special occasion * Convention (meeting), a gathering of individuals engaged in some common interest * Event management, the organization of ev ...
", including how they 'bubble' and the features of the Event object that is passed to most event handlers. # Play well with other scripts by avoiding global function and variable names. # "Work for the next developer" by using self-explanatory variable and function names, creating logical and readable code, making dependencies obvious, and commenting any code that still might confuse. The broader definitions of unobtrusive JavaScript have much in common with general programming best practices, such as encapsulation and abstraction layers, avoidance of
global variable In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global s ...
s, meaningful
naming conventions A naming convention is a convention (generally agreed scheme) for naming things. Conventions differ in their intents, which may include to: * Allow useful information to be deduced from the names based on regularities. For instance, in Manhatta ...
, use of appropriate design patterns, and systematic testing.


References


Further reading

* * * * * * * * {{DEFAULTSORT:Unobtrusive Javascript JavaScript Web accessibility Responsive web design Adaptive web design