Home  >  Article  >  Web Front-end  >  A detailed discussion of script tags in HTML (with code)

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

不言
不言Original
2018-09-06 17:32:158247browse

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

//第一种方法:直接在标签内使用 javascript 即可
<script>
    console.log('第一种使用方法');
</script>

//第二种方法:引用外部文件
<script src="example.js"></script>

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.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="example1.js"></script>
        <script src="example2.js"></script>
    </head>
    <body>
        <div>页面的内容区域</div>
    </body>
</html>

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.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <div>页面的内容区域</div>
        <script src="example1.js"></script>
        <script src="example2.js"></script>
    </body>
</html>

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.

//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>

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

example1
null
example2
null

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.

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

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.

<!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>

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.

<!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>

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

##