Completion requirements
The second way is to place JavaScript code in an external file and import it into the document. The second method is preferred because, with large programs, it is hard to maintain everything in an HTML document. JavaScript files have the file extension ".js". An example is:
<script src="myprogram.js"></script>
Although all JavaScript code should be in a .js file because our programs are short, we'll place code directly within a <script>
tag. We'll use external .js files in the next course.
Examples
Module fallback
Browsers that support the module
value for the type
attribute ignore
any script with a nomodule
attribute. That enables you to use module scripts while also providing nomodule
-marked
fallback scripts for non-supporting browsers.
<script type="module" src="main.js"></script> <script nomodule src="fallback.js"></script>