Students who have learned a little bit about web page writing all know that the js code in the web page should be written in the script tag, but why do you do this? This article will introduce the script tag.
script tag
Initially, Netscape wanted to display HTML files using js in the browser, but did not want to affect the display effect of this file in other browsers, so it created script tag
We all know that tags can add attributes to it. The script tag has the following attributes:
- async means to download the script immediately and should not hinder other operations in the interface
- src indicates the external file to execute this script, optional
- charset The character set of the external file specified by src
- defer The script is delayed until the document parsing and display is completed, optional
- language has been obsolete
- type is important, used to replace the language attribute, the default is text/javascript, so this parameter generally does not need to be specified.
<script> // 属性设置 function SayHi(){ alert("Hi"); } </script>
<!-- 注意下面的写法 /> 这么写本身没有问题,但是不能在html中使用这种语法,因为这样会打乱html的解析 --> <script></script> // 必须写成下面的样子 <script></script>
Note:
- Until the interpreter evaluates the code in the script tag, the rest of the content on the page will not be loaded or displayed by the browser
- If there is js code in the tag with src, it will be ignored, so if you write src, you should not write code in it.
- Try to use external files to write js, Try to Use less embedded js, which makes maintenance easier and the js is in one place. If two pages use the same js, it can be cached
script tag location
Originally, the tag should be placed in the header, but because it is placed in the header, the page will not be rendered when js is loaded (the page will not start rendering until it is loaded into the body), causing the page to appear for a long time. Bai, so now we put it at the end of the body
... <script></script> <script></script> <script></script>
After adding the defer attribute to the script, putting it in the header can theoretically achieve the purpose of delayed execution, but because different browsers have different support for defer ( The execution order is uncertain, embedded js does not support defer, etc.), it is best for us to follow the above writing method and write it at the end of the body .
The async attribute is also to solve the problem of asynchronous loading of scripts. It also does not support embedded js scripts. Originally, the difference between it and defer is that defer loads in order, async does not load in order, but in actual use, Browsers also have different support for this attribute. Neither attribute can guarantee the sequential execution of scripts and execution before DOMContentLoaded, so it is best not to use this attribute until the browser fully supports it.
tag
When the browser disables js or the browser does not support js at all (of course this is rare now), it needs to be degraded smoothly, so the noscript tag
is needed The function is to display the content in noscript when the situation mentioned above occurs. See the example below
<script></script> <noscript> <p>本页面需要浏览器支持(启用)JavaScript</p> </noscript>
Note that if the browser has JS enabled, the content in this tag will not be displayed.
Recommended tutorials: HTML tutorial, JavaScript video tutorial
The above is the detailed content of Learn about the script tag in HTML. For more information, please follow other related articles on the PHP Chinese website!

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools