JavaScript
x
7
1
<html>
2
<head>
3
<script src="./first.js"></script>
4
<script src="./second.js"></script>
5
</head>
6
</html>
7
In the first.js file, I want to call the functions from second.js:
JavaScript
1
2
1
secondFun(); // calling a function from second.js file
2
This is second.js file:
JavaScript
1
4
1
function secondFun() {
2
console.log('second function called!!')
3
}
4
Advertisement
Answer
tl;dr: Load your dependencies before you depend on them.
You can’t call a function that hasn’t been loaded.
The functions defined in your second JS file won’t be loaded until the first file has finished running all the top-level statements.
Reverse the order of your script elements.