How do you load one JavaScript file from another JavaScript file, like CSS? In CSS we use write @import url("mycss.css");
.
Is there any way to do it in JavaScript?
As long as both are referenced by the web page, yes. You simply call the functions as if they are in the same JS file.
There's no @import-ish function/method in Javascript, but you can simply append a script to the <head>
tag like so:
var newScript = document.createElement('script'); newScript.type = 'text/javascript'; newScript.src = '/path/to/js/file'; document.getElementsByTagName('head')[0].appendChild(newScript);
Or like Edgar mentioned right before me you can use jQuery.
Yes. If you want in pure JavaScript, you have to create dynamically a script
tag, and append it to the document.
Yes. If you use the jQuery library, you can use the $.getScript
method.
$.getScript("another_script.js");
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