I write a lot of jQuery code on the page and decided to transfer some of the functions to a separate file. So, file No. 1 has only function definitions:
function doSomething() { ... } function doSomethingElse() { ... }
On the main page, I placed my jQuery at the end of the file - following the optimization methods found in Zakas book "High Performance Javascript".
So, at the very end of the file (but before the closing body tag) I have:
<script type="text/javascript" src="/scripts/MyFunctions.js"></script> <script type="text/javascript"> doSomething(); doSomethingElse(); .... </script>
Note that the final Javascript scripts are loaded - and run - after the page loads and the DOM declaration. However, I get an error every time it says "doSomething" undefined.
This works without problems if functions are included in the main script. Only when I pull them out do I get this error. Now I believe that the first file was not fully downloaded / compiled when the second makes its calls. But it seems to me that the last script (essentially acting in the "Ready" role) should be compiled last. When I look at jQuery and other JS files, they start with anonymous functions, and not just start right into the functional definitions, so itโs clear that something else is happening.
Please note that I do not think that this is a file upload / search problem: I tried different paths to make sure the path is correct, and this did not fix the problem.
Does anyone know why my features are not found?
Update. If I take the contents of the file exactly as they are found and put it in the first script tag, rather than loading it from the file, this works fine. But I have repeatedly checked the script file name, and that is correct. So this is the process of loading it from an external file.
UPDATE: Resolved. I pulled it through FireBug, as suggested by Keegan and dskrepps, and it hit me right away. I had an ASP.NET variable that I placed in scripts, but of course ASP.NET does not parse the file or replace the variables. When Javascript saw these pluggable values, it was dying from a parsing error that was not reported. So - the file did NOT load, and there is no problem with calling the Javascript function at all. Instead, it was a mistake between the keyboard and the seat .; -)
Thanks to everyone who helped!