Languages
[Edit]
EN

HTML - load JavaScript file

3 points
Created by:
Blythe-F
650

In this article, we would like to show you how to load JavaScript files into HTML.

Quick solution:

<!DOCTYPE html>
<html>
<head>
  <script src="/path/to/script.js"></script>
</head>
<body>
  Web page content here ...
</body>
</html>

Where: script is loaded before web site is presented.

or:

<!DOCTYPE html>
<html>
<body>
  <script src="/path/to/script.js"></script>
</body>
</html>

Where: script is loaded when body is displayed.

or:

<!DOCTYPE html>
<html>
<body>
  <script type="module">
  
    import '/path/to/script.js';
  
  </script>
</body>
</html>

 

Practical example

In this example, we create index.html file that loads index.js file.

index.html file:

<!DOCTYPE html>
<html>
<body>
  <script src="index.js"></script>
</body>
</html>

index.js file:

console.log('The index.js file has been loaded correctly.');

Result:

HTML - load JavaScript file
HTML - load JavaScript file

References

  1. <script>: The Script element - HTML: HyperText Markup Language | MDN

Alternative titles

  1. HTML - import JavaScript script
  2. HTML - attach JavaScript file
  3. HTML - attach *.js file
  4. HTML - import *.js script
  5. HTML - import *.js script
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join