I have this file include in my html and I want to call it from another javascript. Please suggest me how should I do it?
<script type="text/javascript" src="js.js">/* old:123 new: 456*/ </script>
I want to include it in my js file, not in the html.
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.
Dynamic loadingThose files can be loaded asynchronously in JavaScript. To load a JavaScript file dynamically: Create a script element. Set the src , async , and type attributes.
Answer: Use the export and import Statement Since ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes or any other entity to/from other JS files.
If you want to include a JS file in a JS you can use jQuery.getScript() for that
$.getScript('another_file.js', function() { //script is loaded and executed put your dependent JS here });
And if you cannot use jQuery
var imported = document.createElement('script'); imported.src = '/path/to/imported/script'; document.head.appendChild(imported);
Source
For more information about the current CSP3 spec from W3 look here.
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