HOME

TheInfoList



OR:

Unobtrusive JavaScript is a general approach to the use of client-side
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
in
web page A web page (or webpage) is a World Wide Web, Web document that is accessed in a web browser. A website typically consists of many web pages hyperlink, linked together under a common domain name. The term "web page" is therefore a metaphor of pap ...
s so that if JavaScript features are partially or fully absent in a user's
web browser A web browser, often shortened to browser, is an application 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 scr ...
, 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 Front-end web development is the development of the graphical user interface of a website through the use of HTML, CSS, and JavaScript so users can view and interact with that website. Tools used for front-end development There are several tool ...
. 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, while users with additional browser features or faster Internet access r ...
to support
user agent On the Web, a user agent is a software agent responsible for retrieving and facilitating end-user interaction with Web content. This includes all web browsers, such as Google Chrome and Safari A safari (; originally ) is an overland jour ...
s 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 pu ...
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 Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
), the style sheet ( CSS), client-side JavaScript, 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 developer A web developer is a programmer who develops World Wide Web applications using a client–server model. The applications typically use HTML, CSS, and JavaScript in the client, and any general-purpose programming language in the server. is used ...
s. 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 Usability can be described as the capacity of a system to provide a condition for its users to perform the tasks safely, effectively, and efficiently while enjoying the experience. In software engineering, usability is the degree to which a softw ...
: An unobtrusive DOM script does not draw the attention of the user—"visitors just use it without thinking about it." # Graceful degradation: 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 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 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", 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 layer In computing, an abstraction layer or abstraction level is a way of hiding the working details of a subsystem. Examples of software models that use layers of abstraction include the OSI model for network protocols, OpenGL, and other graphics libra ...
s, 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, meaningful
naming conventions A naming convention is a convention (norm), 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 ins ...
, use of appropriate
design patterns ''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a fore ...
, and systematic testing.


References


Further reading

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