Consider the two following files:
// main.js
Somescript();
Call the mod.js here
Somescript();
and
//mod.js
alert('a');
How can I call the file mod.js to run in main.js file ?
Consider the two following files:
// main.js
Somescript();
Call the mod.js here
Somescript();
and
//mod.js
alert('a');
How can I call the file mod.js to run in main.js file ?
Implement those "files" as modules and bundle them using a module bundler like browserify or webpack.
The simple solution is to make your mod.js
module globally available to your main module by adding to the window
object:
// mod.js, define
window.myModule = () => alert('a');
// main.js, execute
window.myModule();
You can:
1) Use a library like require.js to dynamically require JS files.
2) Define your mod.js code wrapped inside a main function. Import inside the page your mod.js before your main.js and inside your main simply call your function defined in the mod.js
<script type="text/javascript" src="mod.js"></script>
<script type="text/javascript" src="main.js"></script>
Your mod.js would be something like
function myFunction() {
// all the mod.js code I want to execute
}
Your main.js:
Somescript();
myFunction();
Somescript();