ArticleslgStudy

science

Unobtrusive JavaScript

Unobtrusive JavaScript is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Unobtrusive JavaScript rather than just read about it. In short: Unobtrusive JavaScript is a general approach to the use of client-side JavaScript in web pages so that if JavaScript features are partially or fully absent in a user's web browser, 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.

Key takeaways

  • Unobtrusive JavaScript belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Unobtrusive JavaScript to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Unobtrusive JavaScript from memory before moving on to harder problems.

Reference excerpt

Unobtrusive JavaScript is a general approach to the use of client-side JavaScript in web pages so that if JavaScript features are partially or fully absent in a user's web browser, 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, while other writers have used the term more precisely to refer to the use of progressive enhancement 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 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 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 (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 not affecting 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". 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 namespaces 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: 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":

"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 layers, avoidance of global variables, meaningful naming conventions, use of appropriate design patterns, and systematic testing.

References

Further reading

Worked examples

Example 1 — a first encounter with Unobtrusive JavaScript

Start with the simplest possible case. Write down what Unobtrusive JavaScript claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Unobtrusive JavaScript before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Unobtrusive JavaScript ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Unobtrusive JavaScript

In research
Unobtrusive JavaScript appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Unobtrusive JavaScript in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Unobtrusive JavaScript is common in secondary-school and first-year university syllabi. It links to neighbouring topics Adaptive web design, JavaScript, Responsive web design, so understanding it makes those chapters shorter.
In everyday life
Look for Unobtrusive JavaScript outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Unobtrusive JavaScript” →

How to study Unobtrusive JavaScript in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Unobtrusive JavaScript means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Unobtrusive JavaScript out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Unobtrusive JavaScript in simple terms?

Unobtrusive JavaScript is a general approach to the use of client-side JavaScript in web pages so that if JavaScript features are partially or fully absent in a user's web browser, then the user notices as little as possible any lack of the web page's JavaScript functionality. The term has been use…

Why does Unobtrusive JavaScript matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Unobtrusive JavaScript?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Unobtrusive JavaScript.

Tags

  • Adaptive web design
  • JavaScript
  • Responsive web design
  • Web accessibility

Keep exploring