How it works...

We saw in previous recipes that a script tag with the attribute type="module" will be executed and treated like an ES module. Browsers that don't support ES modules will not execute this script at all.

If we were to insert a normal script tag, it would be executed by newer browsers as well. In order to avoid running duplicated code we use the nomodule attribute. This tells newer browsers that support ES modules to ignore ID.

Thus, we get the desired behavior. The module tags are executed by compatible browsers and ignored by older browsers. nomodule-attributed scripts are ignored by ES module-compatible browsers and executed by legacy browsers.

 At the time of writing, nomodule is an experimental feature and is not supported ...

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.