📜  HTML |<script> defer Attribute(1)

📅  最后修改于: 2023-12-03 15:15:39.191000             🧑  作者: Mango

HTML | <script> defer Attribute

The HTML <script> tag is used to define a client-side script. Scripts are used to create dynamic content, add interactivity or perform some processing on the data entered in the web page.

The defer attribute is a Boolean attribute that tells the web browser that the script should only be executed after the document has been parsed. This attribute is used to improve the loading speed of the webpage.

Here is an example of how the defer attribute is used in the <script> tag:

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script defer src="script.js"></script>
</head>
<body>
	<h1>Deferred Script Example</h1>
	<!-- Page Content -->
</body>
</html>

In this example, the script.js file will only be executed after the HTML document has been parsed. This means that the rest of the page content will be loaded first, making the page load faster for the user.

Note that the defer attribute is only applicable when the script is loaded from an external file using the "src" attribute. It won't have any effect if the script is embedded in the HTML code.

Moreover, it is important to keep in mind that the defer attribute may not be supported by all browsers. Therefore, it is recommended to test the webpage on different browsers before deploying it to production.

In conclusion, the <script> defer attribute is a useful tool for improving the performance of web pages by preventing the script from blocking the rendering of the page. However, it should be used carefully and tested thoroughly before deploying it to production.