How to Defer Inline Scripts in JavaScript?

Estimated read time 1 min read

Deferring inline scripts in JavaScript can be done by removing the scripts from the HTML and adding them to the bottom of the page or to an external JavaScript file.

For example, instead of having the following inline script in the HTML head:

<head>
  <script>
    console.log("Hello, World!");
  </script>
</head>

You can add it to the bottom of the HTML body or to an external JavaScript file:

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

And in the external JavaScript file (script.js), you have:

console.log("Hello, World!");

This way, the script will be loaded after the rest of the page has finished loading, improving the page load time.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply