I had a character at the end of the script1.js
, which I didn't see, resulting in a syntax error.
Define JavaScript function for multiple script tags
-
19-07-2023 - |
Question
I have a function f1()
defined within a given script script1.js
and another function f2()
which calls f1()
in another script script2.js
. I have included the scripts in my HTML file header like so:
<script src="script1.js"></script>
<script src="script2.js"></script>
I get a ReferenceError
.
f1 is not defined
Could anyone explain why? I guess the function is out of scope since it is in another script. I have included it above the calling function's script, but that did not seem to work. I read something about namespacing, but I don't believe this should be that complicated. Any suggestions?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow