1

I have two .js files:

Card_API.js script.js

I want to be able to call functions in Card_API.js from script.js.

My references in the HTML file look like this:

<script type="text/javascript" src="js/Card_API.js"><\script>
<script type="text/javascript" src="js/script.js"><\script>

in the Card_API.js file, I have a function:

function sortCardsByMana(array, reverse){
    // Do Stuff
}

I want to be able to call the above function in the script.js file, but when I try it like this:

sortCardsByMana(cards, false);

...it gives an error saying that the function is not defined.

Can anyone tell me what I'm doing wrong or if I it's possible?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
James F
  • 94
  • 12
  • 2
    Make sure that you are including `Card_API.js` with a script tag before `script.js`. Otherwise, `script.js` would not be able to call those functions because they wouldn't exist at the time that the file is loaded. – Nick McCurdy Jul 19 '14 at 04:41
  • http://stackoverflow.com/questions/24546142/how-i-can-call-a-function-from-external-javascript-file-in-another-external-java24546171#24546171 – Govind Singh Jul 19 '14 at 04:53

2 Answers2

1

This should work fine—just make sure that the .js file making the call is included in the webpage second.

(When the browser is looking through script.js and sees sortCardsByMana(cards, false);, it has no idea what you're talking about—yet!)

username tbd
  • 9,152
  • 1
  • 20
  • 35
  • That's what I've done but it's refusing to recognize the function. Do I need to be hosting the files on a server or does it work locally too? – James F Jul 19 '14 at 05:38
1

include Card_API.js before script.js .

Ruchir Gupta
  • 990
  • 3
  • 10
  • 20