Chapter 06 of 09Part 2 · Layout

Progressive enhancement: what JavaScript is for

Make the page work with HTML first, then let JavaScript add extras. What scripts should and should not be trusted with.

8 minBy Zuzo StudioUpdated

9 min

In this chapter you will

  • explain progressive enhancement as layers of HTML, CSS and JavaScript
  • decide which jobs belong to HTML and which to JavaScript
  • write a small script that checks for support before it adds a feature

Before you startThis builds on Responsive web design, from phone to wide screen.

Check your understanding

3 questions

Q1A site's contact form only submits through a JavaScript fetch call, and the form has no action. What happens if the script fails to load?

Show the answer

The visitor cannot send a message at all The core job of the page depended on the script. A form with a real action would still submit, and JavaScript could improve it when available.

Q2Which is the best example of feature detection?

Show the answer

Checking if ('share' in navigator) before showing a Share button It tests for the exact thing the code needs, so the button appears wherever sharing works and nowhere else.

Q3What does the defer attribute on a script tag do?

Show the answer

Downloads the script alongside the HTML and runs it after the page has been parsed The script does not block the HTML from being read and rendered, which is why defer is a good default for page scripts.

Key takeaways

  1. Build the core job of a page in HTML, style it with CSS, and use JavaScript only to make it better.
  2. Scripts fail for ordinary reasons, like slow connections, errors and blockers, far more often than because JavaScript is switched off.
  3. Links go somewhere and buttons do something; a div with a click handler is neither.
  4. Use feature detection and @supports to add a feature only where the browser can handle it.
  5. Load scripts with defer so they never hold up the first render of the page.
Up next · Chapter 07 · 9 min Web accessibility basics: pages everyone can use What WCAG asks for, and the structure, contrast and keyboard checks that catch most accessibility problems.

02FAQ

Questions on this chapter

Something else on your mind? Email the studio and a designer, not a salesperson, replies within a day.

What is progressive enhancement?

It is a way of building websites in layers: the content and core actions work with plain HTML, CSS improves how it looks, and JavaScript adds extra behaviour where the browser supports it. If a layer fails, the page still does its main job.

What is the difference between progressive enhancement and graceful degradation?

Progressive enhancement starts from a working baseline and adds features on top. Graceful degradation starts from the full experience and adds fallbacks for browsers that cannot handle it. Both aim for pages that work everywhere, but starting from the baseline makes the fallback the first thing you build.