I have a code where I have nested <script> tag, the inner script tag does nothing but loads another Javascript file using src attribute. Because, How in my code, the inner script tag is closing the outer script.
Since I am already inside JavaScript mode, is there a way to load another script without using <script src=""> call?
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "path_to_the_file.js";
document.body.appendChild(js);
but this is asynchronous, so you have to wait until the script is loaded to use it -
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "path_to_the_file.js";
js.onload = function() {
//Code using this script here
};
document.body.appendChild(js);
Thanks @Fletch, my ES2015 equivalent:
document.body.appendChild(Object.assign(document.createElement('script'), {
type: 'text/javascript',
src: 'http://external.script.url',
onload: () => this.onScriptLoaded()
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With