A detailed discussion of script tags in HTML (with code)
This article brings you a detailed discussion of the script tag (with code) in HTML. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
script element
The main way to use the "Javascript" language in HTML pages is to use the script element. The code inside the script element is executed in sequence from top to bottom.
When multiple script elements are introduced, the browser will parse the script elements in the order they appear on the page. When the previous parsing is completed, the next script element will be parsed. Content
Two ways to use Javascript in HTML
1 2 3 4 5 6 7 |
|
Attributes of the script element
Several commonly used attributes of the script element
src: Optional, used to reference external javascript files
type: Optional, the type of scripting language used to write code (also a MIME type), default The value is text/javascript
async: optional, asynchronous loading of scripts, only valid for external script files
defer: optional, delayed Script loading, executed after the document is completely parsed, is only valid for external script files
The position of the script element in HTML
Since the "Javascript" language is a This single-threaded language can only execute one task at the same time, so the next task can only be carried out after the previous task is completed. This will cause the script element to be in different positions in HTML and show different effects.
All script elements are placed in the element
This approach means that we must wait for all Javascript code to be executed before the display can begin As for the content of the page, if the page has a lot of Javascript code, this method will cause us to see that the page loading will be very slow and the user experience will be very poor. So how to optimize it in this way? it's actually really easy.
1 2 3 4 5 6 7 8 9 10 11 |
|
All script elements are placed behind the page content
To optimize the slow page loading problem mentioned above, we only need to put the Javascript code we use After the content of the page, the page will first load the content and then display it, and then execute the Javascript code, so that the user will not wait for a long time before the page displays the content.
1 2 3 4 5 6 7 8 9 10 11 |
|
Delayed loading of scripts
How to delay loading of scripts requires the use of the defer attribute of the script element. When the element uses the defer attribute, the script will be delayed until the entire page is parsed. Execute after completion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
You will find that the console will print out the following results when the defer attribute is not added
1 2 3 4 |
|
When the defer attribute is added to an element, the result will change, which can be found in the p element The Javascript code will not be executed until the content is loaded.
1 2 3 4 |
|
Asynchronous loading of scripts
Asynchronous loading of scripts requires the use of the async attribute from the script element. It is similar to the defer attribute and both modify the loading behavior of the script element. However, the async attribute does not It will affect other loading of the page and will not block document rendering, and scripts with async attributes cannot guarantee the order in which they are executed, which is different from the defer attribute.
In other words, the code in example2.js may be executed before the code in example1.js, so when using the async attribute, avoid mutual dependence between the two js.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
noscript element
Early browsers will have another problem, that is, how to display page content when the browser does not support the Javascript language. The solution for this is to create a noscript element , which can display content in browsers that do not support Javascript, and will only display its content in browsers that do not support Javascript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Related recommendations:
What is the role of the script tag in HTML? What is the usage of type attribute in script tag?
Research on script tag in HTML_html/css_WEB-ITnose
##

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics





Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
