2

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.

1
  • 1
    I have a feeling it has something to do with the fact that I'm calling and defining the function (in the separate js files) within my $(document).ready(function(){ ; but I'm not sure how to fix this. Commented Nov 5, 2010 at 20:58

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have done this already, I get the alert back but get an error saying "functionName1 is not defined"
There is proablly a syntax error in the function body. Can you post the code for Function1?
+1 I'm new to JavaScript This helped me thx bro "If it does not alert you, you have a syntax error further up your script."
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.