For my CSS I’m building several smaller files for specific functions and then using minification to combine and compress them into one file for download.
I would like to do the same with my JS Files – logical separation and then combine and compress with MIN for download.
The only catch I have is the AJAX based site I’m building has one core JS file that takes requests to load pages in the main viewing area.
Am I able to call this function (which is in one JS file) from other JS files? Its kind of like making one JS file public to the other scripts.
Any advice?
thx
Update******
So how would I call a function in one JS file from another.
eg:
File A has function callmetoday()
How do I call this from within JS file number 2?
Yes. Unless you declare a function or variable inside a function or as an object field, it resides in the global scope. In case of a browser, that’s the
windowobject.Note also the warning by Darwayne: you can only use a function from a different file after it has been declared (and in some browsers even within a file).
Example:
file 1.js:
file 2.js:
will work:
f1appears in the global scopef2appears in the global scopef1andf2are accessible.