Table of Contents
script element
Attributes of the script element
The position of the script element in HTML
Delayed loading of scripts
Asynchronous loading of scripts
noscript element
Home Web Front-end HTML Tutorial A detailed discussion of script tags in HTML (with code)

A detailed discussion of script tags in HTML (with code)

Sep 06, 2018 pm 05:32 PM
html html5 javascript

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

//第一种方法:直接在标签内使用 javascript 即可

<script>

    console.log('第一种使用方法');

</script>

 

//第二种方法:引用外部文件

<script src="example.js"></script>

Copy after login

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

<!DOCTYPE html>

<html>

    <head>

        <title></title>

        <script src="example1.js"></script>

        <script src="example2.js"></script>

    </head>

    <body>

        <div>页面的内容区域</div>

    </body>

</html>

Copy after login

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

<!DOCTYPE html>

<html>

    <head>

        <title></title>

    </head>

    <body>

        <div>页面的内容区域</div>

        <script src="example1.js"></script>

        <script src="example2.js"></script>

    </body>

</html>

Copy after login

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

//example1.js 中的代码

//console.log('example1');

//console.log(document.getElementById('content'));

 

//example2.js 中的代码

//console.log('example2');

//console.log(document.getElementById('content'));

 

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Title</title>

    <script defer src="example1.js"></script>

    <script defer src="example2.js"></script>

</head>

<body>

    <div id="content">这里页面的内容</div>

</body>

</html>

Copy after login

You will find that the console will print out the following results when the defer attribute is not added

1

2

3

4

example1

null

example2

null

Copy after login

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

example1

<div id="content">这里页面的内容</div>

example2

<div id="content">这里页面的内容</div>

Copy after login

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

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Title</title>

    <script async src="example1.js"></script>

    <script async src="example2.js"></script>

</head>

<body>

    <div id="content">这里页面的内容</div>

</body>

</html>

Copy after login

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

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

    <script async src="example1.js"></script>

    <script async src="example2.js"></script>

</head>

<body>

    <noscript>

        当前浏览器不支持 Javascript 请更换浏览器

    </noscript>

</body>

</html>

Copy after login

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

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

See all articles