Load JavaScript script file after page load

 

First create HTML element then add source of required JS file to HTML object.

 

const scriptOBJECT = document.createElement('script');

scriptOBJECT.setAttribute('src','exp.js');


scriptOBJECT.onload = function handleScriptLoaded() {
  
  //do your code here

};

scriptOBJECT.onerror = function handleScriptError() {

	//error occurs while loading file
  	console.log('Some thing went wrong unable to load JavaScript file');
};

document.head.appendChild(scriptOBJECT);

Tags:

Share:

Related posts