ECMAScript Cookbook
上QQ阅读APP看书,第一时间看更新

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 by all browsers. However, it may be supported in the future.