-1

I have two JavaScript files in a web application project and want a function in one file to call a function from the other. How can that be done?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • In your html file load, you presumably load in both javascript file and then run when document is ready. – dchhetri Jan 08 '13 at 21:29
  • Take a look at -> http://stackoverflow.com/questions/950087/include-javascript-file-inside-javascript-file – nanobash Jan 08 '13 at 21:32
  • @DaHaKa Thanks for the link. I see there that one might not be able to call the other immediately because the loads are asynchronous. Did I understand correctly? – ispiro Jan 08 '13 at 21:38
  • Honestly I've not read all information , but simple ways to accomplish your task is to include files like it is shown in the answer and second , load functions with Ajax – nanobash Jan 08 '13 at 21:41

2 Answers2

3

Refer to both files from the HTML page

<script src="js/file1.js"></script>
<script src="js/file2.js"></script>

functions in one file should be accessible from functions in another file.

Andrew
  • 7,619
  • 13
  • 63
  • 117
  • Thanks. Works great. I didn't think the solution would be dependent on the html so was trying through the js files themselves. – ispiro Jan 08 '13 at 21:32
  • According to the link in [DaHaKa's comment](http://stackoverflow.com/questions/14224352/javascript-use-script-from-other-javascript-file#comment19731117_14224352), though, it seems one js might not be able to call the other immediately because the loads are asynchronous. (If I understand correctly.) – ispiro Jan 08 '13 at 21:41
  • It purely depends on your java script. You need to design it accordingly to page load. – Andrew Jan 08 '13 at 21:42
1

When both JS files are parsed by a browser functions from one file will be visible to another and vice versa.

Please bear in mind that the JavaScript file which should be used in another JavaScript file usually needs to be declared before the other JavaScript file (using the HTML <script> tag).

416E64726577
  • 2,214
  • 2
  • 23
  • 47
Alexandar
  • 916
  • 2
  • 9
  • 22