Home » Defer Parsing of JavaScript – 100% Working [Issue Solved]
Speed Up

Defer Parsing of JavaScript – 100% Working [Issue Solved]

Are you looking for the solution of Defer Parsing of JavaScript error? You will get your answer in this tutorial. The script I will provide is 100% working. Also, it is safe and easy to place on your website.

Defer Parsing of JavaScript

How to Defer Parsing of JavaScript?

First of all, you have to open your website’s HTML file and place the following code in <HEAD> section.


<script>
function parseJSAtOnload() {
var element = document.createElement("script");
element.src = "your_script.js";  //<-- Enter the script which you want to defer.
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", parseJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", parseJSAtOnload);
else window.onload = parseJSAtOnload;
</script>

Defer Multiple Javascript Files in One Script

This script is used to defer multiple files of js. Just need to place this code in your HTML file.
Note: Replace your original scripts with your_script1.js, your_script2.js, your_script3.js and so on.


<script>
function parseJSAtOnload() {
var links = ["your_script1.js", "your_script2.js", "your_script3.js"],
headElement = document.getElementsByTagName("head")[0],
linkElement, i;
for (i = 0; i < links.length; i++) {
linkElement = document.createElement("script");
linkElement.src = links[i];
headElement.appendChild(linkElement);
}
}
if (window.addEventListener)
window.addEventListener("load", parseJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", parseJSAtOnload);
else window.onload = parseJSAtOnload;
</script>

As a result, your error will be removed from the speed test tool. This method is very safe and easy to install. It will not harm your website design or functionality. Another advantage is, it is supported in all frameworks.

Is this article helpful? If yes, please share it.