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
click
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.
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 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!