Why can't we solve accessibility with a one line script tag?
Tags: a11y, webdev
Published: 2021-08-07 04:29:38

This is a good question. There are some companies that'd like to beg to differ (such as AccessiBe, which is rubbish). This post is not for them. It's for you. They know what they're doing (and not doing).

{% twitter 1421660430217490433 %}

## To start: the web is (generally) accessible by default.

We break it. Me, you, companies, capitalism, you name it - that's what breaks the web from being accessible.

`<div>` tags with an `onClick` event when the `<button>` element is right there, modals that don't trap focus, and other examples are rampant throughout the Internet.

Let's think about this from the angle of how your code is interpreted by the browser and accessibility software:

1. Write some HTML.
2. Load that HTML into a browser.
3. The browser interprets that HTML, and creates an accessibility tree out of it (among rendering it, etc)
4. The browser exposes an API that accessibility technology accesses, and a browser interprets the HTML as if it was written to _standards_.
  - If this HTML is not up to spec, such as being used improperly (looking at a `<div>` with an `onClick` event, for example), the browser cannot infer what exactly that is for.
  - This affects the accessibility technology, as it can only do with what the browser provides.

If we use web technologies improperly, do we expect a browser to somehow perform heuristics to get what we mean? After all, programming is _literally_ telling a computer what to do in a language it understands. If we do not tell it what to do in a way it understands, it will _generally_ take the safest path.

An example of this is correcting for when a closing HTML tag is missing. In older browser days, the standard allowed for certain tags to omit a closing tag and that was _valid_ HTML. That's not the case anymore (except for certain exceptions, like `<input>`!).

Take this code:

```
<div>
  <span>Hello!
</div>
```

In any modern browser, the `<span>` above will be automatically closed in the DOM if you inspect it.

![Screenshot from Firefox's Inspector showing the DOM with a span that has a closing span tag, yet the source does not have that closing tag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3mpksmvclqrdb8dgfs0.png)

In the source above, there's not even an HTML tag, HEAD tag, or BODY tag, yet Firefox assumes it anyway. The browser can help us in many ways, but it cannot get our intent out of our badly formed code.

## Sometimes, the standards aren't all we need

In other instances, we can correctly use all HTML tags and still not make an accessible interface. This is where the nuance comes into play. For example, a modal. By definition, a modal is simply a dialog over top other elements. The user experience dictates that this is treated as a separate "document", and there's no standard way to do this. Modals in general break the way the web was created. A page is a "document", yet a modal is a "document within a document."

If a document is to really behave like a document, then it must be the sole document "in focus." With modals, we generally add a "focus trap" to make sure that users cannot get out of it unwillingly. A bolt-on script will not know this is what you intended.

There are some instances where we _need_ JavaScript in order to make the web accessible. Stephanie Eckles [wrote a really good Smashing Magazine article](https://www.smashingmagazine.com/2021/06/css-javascript-requirements-accessible-components/) that expands on this.

## How does someone's JavaScript know this nuance?

It doesn't. It can't. It won't. And users that need accessible interfaces and experts in the field have said [time](https://overlayfactsheet.com/) and [time](https://nfb.org/about-us/press-room/national-convention-sponsorship-statement-regarding-accessibe) [again](https://twitter.com/HabenGirma/status/1390051536819023873?s=20) that [it](https://adrianroselli.com/2020/06/accessibe-will-get-you-sued.html) [doesn't](https://twitter.com/toddlibby/status/1421396502052839429?s=20) [work](https://www.nbcnews.com/tech/innovation/blind-people-advocates-slam-company-claiming-make-websites-ada-compliant-n1266720).

## Don't resort to bolt-on fixes

AccessiBe isn't the first company to do this, but it's making big promises and [being slimy in the process](https://adrianroselli.com/2020/06/accessibe-will-get-you-sued.html#Update06). There are [others named](https://overlayfactsheet.com/#statement-from-sponsors-and-signatories-to-this-fact-sheet) in the Overlay Fact Sheet - they should all be avoided.

**Accessibility is not a bolt-on.**