I am trying to dynamically add an external script tag to my html as follows:
html
<html>
<head>
<script src="js/load-scripts.js"></script>
</head>
<body>
<form>
Load scripts NOW!!<br/>
<input type="text" onclick="downScripts(); testCall();" value="Click to Load scripts and test" />
</form>
</body>
</html>
load-scripts.js
function downScripts() {
var element = document.createElement("SCRIPT");
element.src = "js/try-catch.js";
element.defer = false;
console.log(element);
document.body.appendChild(element);
}
When I click on the button, the js/try-catch.js file gets added to the Sources in chrome dev tools, but when I try to call one function from try-catch.js - which is defined in try-catch.js (like testCall() below)
try-catch.js
function testCall() {
alert('scripts imported from try catch js external');
}
If the external javascript has been added (i see it in developer tools), why am i not able to call one of it's functions- I get this error: Uncaught ReferenceError: testCall is not defined