View Full Version : Accessing external as well as internal JS functions
01-11-2004, 04:07 PM
I have several JS functions being called from a .html file. In order to make the file more readable, I decided to cut and paste some of these functions to an external .js file, whilst keeping those which are still not finished in the .html file itself. The problem is that the external functions will execute when called from within the .html file, but those inside the file seem to be inaccessible.
I am using HTML-Kit for coding and testing and get the following message:
Error: Object expected
Using IE shows no error message, but it is clear that the function's code is not being executed.
Is there a means to overcome this. I thought it would be beneficial to place debugged functions in an external file and keep only those that still require attention.
01-12-2004, 05:04 PM
The external file cannot have any HTML code in it and the error you describe sounds like what you've get if you had HTML code in there.
01-12-2004, 07:35 PM
One problem with using external files is that you can not be sure when content in them are going to be executed, because they are loaded asyncronously to the document, including any other scripts that either are embedded or external. The best solution for this problem is to do the variable and function definitions only while loading, and not start actually executing any code until the document has been loaded.
The problem you have sounds like a typical try to execute a function that has not yet been defined, but it may be caused by other reasons.
Why don't you zip it and send it as an attachment? Or even better, leave it on the server and give us the link to it. It's easier to debug if we have the actual code to work at.
01-13-2004, 08:25 AM
Thanks Roy and liorean,
I am sorry that this reply took so long, but for some reason I could neither preview nor submit it. I kept getting the message: "The page cannot be displayed"
Comment tags are easily transferred across by cutting and pasting of code. The text editor that I use generates the <!-- and //--> tags automatically. From your reply I now know, what they were meant to do.
The other important aspect is the point about the asynchronous loading of the .js file versus the .html page. I was not aware of that. By implication an onload ="someFunction()" instruction could potentially lead to problems, i.e. the function not yet being available for execution as the page is loading.
OK, I get your last point about a bug, hidden deep down within a function, placed amongst many other functions in an external .js file. Such a bug will be difficult to find. I will take up your offer to debug the code once I am satisfied with the user interface. Else I will end up fiddling with debugged code.
I am appreciating your guidance and help. Thank you very much.
01-13-2004, 02:48 PM
As for the onload event, the charm of it is that you know all the script files are downloaded and parsed before it triggers.
01-13-2004, 08:11 PM
One of your signature links led me to JS Lint. I have used that one for debugging the functions and will give the others a try as well.
I am aware that wrong values can be sent to the called functions and will put alert's in place to inform the user and stop the particular function call.
Thanks for clearing up the "onload" event misunderstanding I had.