37
loading...
This website collects cookies to deliver better user experience
async
and defer
.As you might already know, external JavaScript files can be included in the:
Before we continue and discuss about these techniques in depth, let's understand what happens when a browser loads a webpage.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript reference inside body</title>
</head>
<body>
<!-- DOCUMENT CONTENT -->
<script src="./src/main.js"></script>
</body>
</html>
<script>
within the HTML body.The simple answer to this is: Add script references inside the head
<head>
.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript reference inside body</title>
<!-- Add script file source here -->
<script src="./src/main.js"></script>
</head>
<body>
<!-- DOCUMENT CONTENT -->
</body>
</html>
<head>
, the script files are fetched before the HTML DOM is parsed and loaded completely. <p>
when user clicks the button. Look what happens when you add the script source in the <head>
.
You get an error "cannot read property addEventListener of null". This happens because the DOM is loaded after JavaScript is fetched, and hence there is no reference to the button.
document.addEventListener('DOMContentLoaded', function() {
btn.addEventListener('click', () => {
p.textContent = "You clicked me!";
});
});
And now if user clicks the buton, there is no error:
This is yet again an old technique.
HTML5 provides two new, modern features that prevents blocking of HTML parse and JavaScript load.
The two attributes: async
and (or) defer
are added to the script tag when it is included in the <head>
.
Both the attributes ask the browser to load the script file in a separate thread without blocking the HTML file from being parsed.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript reference inside body</title>
<!-- Add script file source here -->
<script src="./src/main.js" async></script>
</head>
<body>
<!-- DOCUMENT CONTENT -->
</body>
</html>
<head>
<!-- Add script file source here -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous" async></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous" async></script>
<script src="./src/init.js" async></script>
</head>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript reference inside body</title>
<!-- Add script file source here -->
<script src="./src/main.js" defer></script>
</head>
<body>
<!-- DOCUMENT CONTENT -->
</body>
</html>
async
, script is not executed immediately once the file is loaded, and the DOM load is not blocked. <head>
.<head>
<!-- Add script file source here -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous" defer></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous" defer></script>
<script src="./src/main.js" defer></script>
</head>
<body>
only if the script your website uses is minimal.If you have multiple scripts that are heavy, refer to it within the <head>
as sourcing within the <body>
blocks JavaScript from loading, thereby affecting the performance of your website.
Use async in case the scripts in your website are independent of each other, and you want to execute code before the main JavaScript loads.
Use defer when you have scripts that rely on parsing of HTML and manipulation of DOM elements.