jquery how to call function from external .js file
Solution 1
This should work. The js file is not found Or there is a JS error preventing functionName1 from being defined. If you do not have a JS debugging tool, try adding alerts. start right before the function name:
alert ('made it!')
function functionName1(){ ///DO SOMETHING };
If it does not alert you, you have a syntax error further up your script.
Break this down to the simplist implementation and add the real-work complexity. your error wil be come evident.
Solution 2
Browser JavaScript is not "inside files" : the contents of the files are executed as part of the document itself when the including <script>
tags are encountered. So, if prehomefunctions.js
defines function functionName1
and is included before feedFunctions.js
which calls functionName1
, everything should work.
There's probably something else that you have not explained which is causing your code to break.
sadmicrowave
Updated on June 05, 2022Comments
-
sadmicrowave almost 2 years
I'm trying to figure out the jQuery code syntax for calling a function in a separate .js file still referenced with a script href tag in my head tags. So here is essentially what I have.
<head> <script type="text/javascript" src="/Publish/Depts/EWI/scripts/prehomefunctions.js"></script> <script type="text/javascript" src="/Publish/Depts/EWI/scripts/feedFunctions.js"></script> </head> //Inside feedFunctions.js if(condition){functionName1()}; //Inside prehomefunctions.js function functionName1(){ ///DO SOMETHING };
at this point whenever I try to execute this my feedFunctions.js file throws an error saying that "functionName1" is not a function because the file cannot find this function within itself, but I'm trying to make it point to prehomefunctions.js to find this function.
any help would be much appreciated.