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 Methods for embedding JavaScript code into web pages1,<script><code>Element directly embeds code<pre class="brush:php;toolbar:false">&lt;script type=&quot;text/javascript&quot;&gt; function sayHello() { alert(&quot;hello!&quot;); } &lt;/script&gt;&lt;/h3&gt;2, &lt;h3 id=&quot;script-code-Element-loads-external-script-pre-script-nbsp-type-text-javascript-nbsp-src-example-js-script&quot;&gt;&lt;script&gt;&lt;code&gt;Element loads external script&lt;pre class=&quot;brush:php;toolbar:false&quot;&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;example.js&quot;&gt;&lt;/script&gt;&lt;/h3&gt; &lt;h2 id=&quot;script-code-Tag-relatedAttribute-a-href-http-www-php-cn-wiki-html-target-blank-script&quot;&gt;&lt;script&gt; &lt;code&gt;Tag relatedAttribute&lt;a href=&quot;http://www.php.cn/wiki/169.html&quot; target=&quot;_blank&quot;&gt;&lt;/script&gt;&lt;/h2&gt;type attribute&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;/h3&gt; &lt;ul class=&quot; list-paddingleft-2&quot;&gt; &lt;li&gt;##&lt;script&gt;&lt;p style=&quot;text-align: left;&quot;&gt;The label defaults to JavaScript code, embedded javascript When scripting, the type attribute can be omitted&lt;code&gt;&lt;/script&gt; &lt;/li&gt;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&lt;li&gt;&lt;script&gt;## 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 &lt;p style=&quot;text-align: left;&quot;&gt;&lt;script&gt;&lt;code&gt; node still exists. In the DOM, you can use the text attribute of the &lt;script&gt;&lt;code&gt; node to read its content &lt;code&gt;defer attribute&lt;/script&gt;&lt;/li&gt; &lt;/ul&gt; &lt;pre class=&quot;brush:php;toolbar:false&quot;&gt;&lt;script&gt;&lt;/script&gt; &lt;script&gt;&lt;/script&gt;</pre> <h3 id="The-running-process-of-the-defer-attribute"> The running process of the defer attribute: </h3> <p style="text-align: left;"><strong></strong></p>The browser starts to parse the HTML web page<ol class=" list-paddingleft-2"> <li> <p style="text-align: left;"></p>During the parsing process, # with the defer attribute is found ##<script></script> </li>Element<li> <p style="text-align: left;"><code></code>The browser continues to parse the HTML web page and downloads in parallel </p> <script></script> The external script loaded by the element
The browser has completed parsing the HTML web page, and now goes back to execute the downloaded script
Load resources asynchronously
Execute scripts in sequence
-
External scripts loaded using defer do not You should use the
document .write method -
<script></script> <script></script>
The browser starts to parse the HTML web page
-
During the parsing process, the
<script></script> tag with the async attribute - # is found
tag in parallel##The browser continues to parse the HTML web page and downloads the external script in the
<script></script> 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
- 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); });
- 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
<script>The JavaScript code inside the tag will be parsed from top to next<ol class=" list-paddingleft-2"><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<li><p style="text-align: left;">Advantages of loading external scripts: maintainability, cacheability, adaptability to the future<li><p style="text-align: left;"><code><script> Reasons for placing it at the bottom: 1. To avoid the "blocking effect". 2. Avoid calling <a href="http://www.php.cn/code/6401.html" target="_blank">DOM node before the DOM structure is generated, resulting in an error <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>Recommended reading:<p><a href="http://www.php.cn/js-tutorial-398251.html" target="_blank">How to deal with MySQL database access denial<br/><p><a href="http://www.php.cn/js-tutorial-398247.html" target="_blank">How to implement hexagonal button special effects<br/></script>
The above is the detailed content of Summary of using JS methods in HTML. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
