This time I will bring you a summary of the methods of using JS in HTML. What are the precautions for using JS in HTML? Here are practical cases, let’s take a look. Preface JavaScript is the browser’s built-in scripting language. When a JavaScript script is embedded in a web page and the browser loads the web page, it will execute the script to operate the browser and achieve various dynamic effectsMethods for embedding JavaScript code into web pages1,<code>Element directly embeds code</code><div class="code" style="position:relative; padding:0px; margin:0px;"><pre><script type="text/javascript"> function sayHello() { alert("hello!"); } Copy after login 2, <code>Element loads external script</code><div class="code" style="position:relative; padding:0px; margin:0px;"><pre><script type="text/javascript" src="example.js">Copy after login <code>Tag related</code>Attribute<a href="//m.sbmmt.com/wiki/169.html" target="_blank"></a> </h2>type attribute<h3 style="text-align: left;"></h3> <ul class=" list-paddingleft-2"> <li>##<script><p style="text-align: left;">The label defaults to JavaScript code, embedded javascript When scripting, the type attribute can be omitted<code></code></p> </li>If the value of the type attribute is not recognized by the browser, the code will not be executed, so it can be used in<li><script>## To embed any text content in the # tag, just add a type attribute that the browser does not recognize. The browser will not execute or display its content, but the <p style="text-align: left;"><script><code> node still exists. In the DOM, you can use the text attribute of the </code><script><code> node to read its content </code><code></code></p>defer attribute</li> </ul> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre><script src="a.js" defer> Copy after login The running process of the defer attribute: The browser starts to parse the HTML web page During the parsing process, # with the defer attribute is found ##Element The browser continues to parse the HTML web page and downloads in parallel The external script loaded by the elementThe browser has completed parsing the HTML web page, and now goes back to execute the downloaded script Note: Load resources asynchronously Execute scripts in sequence External scripts loaded using defer do not You should use the document.write method async attribute Copy after login async attribute running process: The browser starts to parse the HTML web page During the parsing process, the tag with the async attribute# is found ##The browser continues to parse the HTML web page and downloads the external script in the tag in parallel Script download completed , the browser pauses parsing the HTML web page and starts executing the downloaded script After the script is executed, the browser resumes parsing the HTML web page Note: Asynchronous loading of resources will not execute JS in order. Whoever downloads it first will be executed first. External scripts loaded using async should not use the document.write method Async and defer attribute induction can solve the "blocking effect" all load resources asynchronously, but the order of execution is different If the scripts If there is no dependency, use the async attribute. If there is a dependency between scripts, use the defer attribute Dynamicly generate script ['a.js', 'b.js'].forEach(function(src) { var script = document.createElement('script'); script.src = src; script.async = false; document.head.appendChild(script); });Copy after login No Will block page rendering Async is set to false to ensure that b.js is executed after a.js is loaded after this code The script file will wait until b.ja is executed before executing it Summary of relevant knowledge points included inThe JavaScript code inside the tag will be parsed from top to next<ol class=" list-paddingleft-2"></li><li><p style="text-align: left;">No matter which way the code is embedded, as long as the defer and async attributes do not exist, the browser will process the <code><Script> tags in the order they appear on the page. Analysis</h2> <li><p style="text-align: left;">Advantages of loading external scripts: maintainability, cacheability, adaptability to the future</p></li> <li><p style="text-align: left;"><code><script></code> Reasons for placing it at the bottom: 1. To avoid the "blocking effect". 2. Avoid calling <a href="//m.sbmmt.com/code/6401.html" target="_blank">DOM node</a> before the DOM structure is generated, resulting in an error </p></li> <p> I believe you have mastered the method after reading the case in this article, please pay attention for more exciting information Other related articles on php Chinese website! </p> <p>Recommended reading:</p> <p><a href="//m.sbmmt.com/js-tutorial-398251.html" target="_blank">How to deal with MySQL database access denial</a><br></p> <p><a href="//m.sbmmt.com/js-tutorial-398247.html" target="_blank">How to implement hexagonal button special effects</a><br></p>