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)

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

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(&#39;第一种使用方法&#39;);
</script>

//第二种方法:引用外部文件
<script></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.

nbsp;html>

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

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.

nbsp;html>

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

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'));

nbsp;html>


    <meta>
    <title>Title</title>
    <script></script>
    <script></script>


    <div>这里页面的内容</div>

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>这里页面的内容</div>
example2
<div>这里页面的内容</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.

nbsp;html>


    <meta>
    <title>Title</title>
    <script></script>
    <script></script>


    <div>这里页面的内容</div>

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.

nbsp;html>


    <meta>
    <title>Title</title>
    <script></script>
    <script></script>


    <noscript>
        当前浏览器不支持 Javascript 请更换浏览器
    </noscript>

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

##<script> tag in html You can also link non-js files_html/css_WEB-ITnose<a href="http://www.php.cn/div-tutorial-280058.html" target="_self"></script>

The above is the detailed content of A detailed discussion of script tags in HTML (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn