Home  >  Article  >  Web Front-end  >  Where should js code be placed in HTML?

Where should js code be placed in HTML?

藏色散人
藏色散人Original
2018-08-14 16:24:426525browse

When we write html program code, we often need to reference js code. So for novices in web development, if they want the js code in html to work properly, where should it be placed? This article will introduce to you the placement of js code in HTML and the different methods of referencing js code in HTML.

1. The internal js code can only be placed in three locations: the tag in

  1. ##html Among them is the head header, any position in the tag in <script>js code</script>


  2. ##html, <script>js code</script>
  3. is written directly in the tag in the body,

    click

2. Method of referencing js external files:

Quote js code in head

<script src=".."  type="text/javascript"></script>

The src attribute specifies the URL of the external script file. Sometimes, we need to run JavaScript in multiple pages of our website. Instead of rewriting the same script, simply create the JavaScript in a separate file, save it with a .js suffix, and reference that file using the src attribute in the <script> tag. Note that external files cannot contain <script> tags! </script>

The type attribute in<script>, the required type attribute specifies the MIME type of the script. </script>

The type attribute identifies the content between the <script> and </script> tags.

Where should js code be placed in HTML?Note: HTML code is read and run from top to bottom

The above is about where the js code is placed and how the js code is placed. This introduction to the relevant knowledge used in HTML and external references to JS has certain reference value. I hope it will be helpful to friends in need.


【Recommended related articles】

The difference between the location of Js in the HTML file


Javascript basics The location of the js code in the web page of the tutorial


Where should the <script> be placed? <p><a href="http://www.php.cn/div-tutorial-374543.html" target="_blank">How to insert content at a specified position in JS<p><a href="http://www.php.cn/js-tutorial-404049.html" target="_blank"></script>

The above is the detailed content of Where should js code be placed in HTML?. 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