Home >Web Front-end >HTML Tutorial >How to connect html to external js

How to connect html to external js

下次还敢
下次还敢Original
2024-04-11 06:25:551180browse

To connect an external JS file, use the <script> tag with the src attribute. Typically, this tag is placed at the bottom of <body> to avoid blocking rendering. Multiple <script> tags can be added to connect multiple external JS files. The src attribute can use a relative path (relative to the HTML file) or an absolute path (starting from the root directory). The tag can also contain other attributes such as async (asynchronous loading) and defer (deferred execution).

How to connect html to external js

How to use HTML to connect external JS files

Introduce external JS files

To connect an external JavaScript file to an HTML document, use the <script> tag and specify the path to the JavaScript file in the src attribute:

<code class="html"><script src="path_to_javascript_file.js"></script></code>

Choose where to place the <script> tag

Normally, the <script> tag is placed in the HTML document <body> section to avoid blocking page rendering:

<code class="html"><body>
  ... <!-- 网页内容 -->

  <script src="path_to_javascript_file.js"></script>
</body></code>

Multiple external JS files

If you need to concatenate multiple external JS files , you can simply add multiple <script> tags in the <body> section:

<code class="html"><script src="path_to_file1.js"></script>
<script src="path_to_file2.js"></script>
<script src="path_to_file3.js"></script></code>

Relative and absolute paths

src The attribute can contain a relative path (relative to the location of the HTML file) or an absolute path (starting from the root of the website):

  • Relative path: js/main.js
  • Absolute path: /path/to/javascript_file.js

Other attributes

##<script> Tags can also contain other attributes, such as:

  • async: Allows scripts to be loaded asynchronously and executed before the page is rendered.
  • defer: Delay script execution until document parsing is complete.

The above is the detailed content of How to connect html to external js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn