To import JS dynamically, you need to consider the onreadystatechange
and load
events that fire when the script is parsed by the browser and available to you. You can use this function:
function getScript(url, callback) { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = url; script.onreadystatechange = callback; script.onload = callback; document.getElementsByTagName('head')[0].appendChild(script); }
And you can use it as follows:
getScript('path to your js file', function(){ alert("Pet Name: " + PETNAME); });
source share