JavaScript defer
Posted by Superadmin on May 03 2023 14:31:25

JavaScript defer

By Priya PedamkarPriya Pedamkar
  

javascript defers

Introduction to JavaScript defer

While running any script on the HTML page, we need to take care of the loading performance of the JavaScript application. The application never is harmed by performance issues. If we add <script> tag within the HTML <head> tag, then application consumes more memory so that application performance declined. Based on where we are adding our script in the HTML page also influences the loading time of the application. So, improve the application performance add the defer attribute within the <script> tag like <script defer src=””>. defer attribute loads required resources immediately and improve the performance. defer is an attribute that is a boolean type.

Why we come into defer Concept?

In general, we are loading JavaScript file in HTML file as like below

Syntax:

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

Syntax:

<body>
//HTML code
<script src="">
</body>

So, instead of doing that latest browser we can achieve the same functionality with deferring attribute within the <script> tag.

Advantages:

How does defer tag Work in JavaScript?

Below are the points explain how defer tag works:

Syntax:

<body>
<script defer src="script.js"></script>
//HTML code
</body>

Like, the defer attribute async attribute also performs the same task for loading JavaScript utilities. But if we declare defer attribute in one <script> file and async attribute in another <script> tag and both are defined inside HTML body JavaScript gives precedence to defer attribute only.

Examples to Implement JavaScript defer

Below are the examples:

1. Defer with Paragraph Content

HTML Code: Diferparagraph.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Differ Attribute</title>
<style type="text/css">
h1 {
text-align: center;
color: green;
}
.p1 {
border-style: solid;
border-color:red;
border-width: 1px;
font-size: 20px;
color: blue;
width: 1000px;
}
.p2 {
border-style: solid;
border-color:blue;
border-width: 1px;
font-size: 20px;
color: fuchsia;
width: 1000px;
}
</style>
</head>
<h1>Defer attribute with Paragraph Text</h1>
<!--Including script file with defer attribute-->
<script src="defer.js" defer></script>
<p class="p1">
defer attribute in JavaScript used for loading the JavaScript utilities
(script files) after loading of the main content of HTML</p>
<br>
<p class="p2">After loading of JavaScript utilities end user no need
to wait for seeing the main content of the page, later defer includes
rest of the files.</p>
</body>
</html>

JavaScript Code: defer.js

alert("I am loading after HTML content?");

Output:

JavaScript defer - 1

Explanation: While executing the application for the first time then it will execute the HTML content first. Later, executes the alert box may be first followed by content because already this application stored in the browser cache.

2. Defer with Buttons

HTML Code: DeferButtons.html

Code:

<!DOCTYPE html>
<html>
<head>
<title>Differ Attribute</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<h1 style="color:green">Defer attribute with Buttons</h1>
<!--Including script file with defer attribute-->
<script src="defer.js" defer></script>
<div class="container">
<button type="button" class="btn">Login</button>
<button type="button" class="btn btn-default">Register</button>
<button type="button" class="btn btn-primary">Fund Transfer</button>
<button type="button" class="btn btn-success">Success</button>
</div>
<br>
<div class="container">
<button type="button" class="btn btn-info">About</button>
<button type="button" class="btn btn-warning">Know more</button>
<button type="button" class="btn btn-danger">Contact Me</button>
<button type="button" class="btn btn-link">Feedback</button>
</div>
</body>
</html>

JavaScript Code: defer.js

alert("I am executed after Buttons executed?");

Output:

JavaScript defer - 2

Explanation: While executing the application for the first time then it will execute the HTML content first. Later, executes the alert box may be first followed by content because already this application stored in the browser cache.

3. Defer with Navigation Bar

HTML Code: DeferNavigation.html

<!DOCTYPE html>
<html>
<head>
<title>Differ Attribute</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<h1 style="color: green">Defer attribute with Buttons</h1>
<!--Including script file with defer attribute-->
<script src="defernav.js" defer></script>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li class="active"><a href="#">EDUCBA</a></li>
<li><a href="#">Courses</a></li>
<li><a href="#">Fee Structure</a></li>
<li><a href="#">Faculty</a></li>
</ul>
</div>
</nav>
</body>
</html>

JavaScript Code: defernav.js

alert("I am executed after Navigation Bar executed?");

Output:

DeferNavigation.html

Explanation: While executing the application for the first time then it will execute the HTML content first. Later, executes the alert box may be first followed by content because already this application stored in the browser cache.