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!

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)
