Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2003
    Location
    Sasolburg, South Africa
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing external as well as internal JS functions

    Hi everybody,

    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:

    Line: 108
    Char:1
    Error: Object expected
    Code: 0

    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.

    Thanks

    Dieter

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Specifically, make sure you remove the <script> and </script> tags and also make sure you get rid of those <!-- and //--> html comment tags as well (in fact you shouldn't need html comment characters any more at all, how many browsers are left which don't recognize javascript?).
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.





    (As for the putting of debugged code in the JavaScript file and undebugged in the document, I'd say you don't realise one of the main points of JavaScript debugging - that a bug in one place of the code may get through undetected and not cause a problem until you actually try to use the related objects, variables of functions. Because of that, you will very seldom find high integrity code, unless you are doing rigorous runtime tests of variables and objects within all your "debugged" code, which would be a waste of processing capability.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New Coder
    Join Date
    Nov 2003
    Location
    Sasolburg, South Africa
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Talking about finding bugs. I am new to JS, but have done a lot of coding in VBA using MS Excel. One of the major benefits was the ability to step through a subroutine or procedure line-by-line, following the program's flow and being able to see the values of variables as they were being calculated. Is there a program available that can do something similar for JavaScript?

    I am appreciating your guidance and help. Thank you very much.

    Regards,
    Dieter

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Venkman, the Mozilla JavaScript Debugger is one of a number of very useful tools within the Mozilla browser, if you like debugging environments. It is not an editor, however. The Mozilla JavaScript Console is a very good bug tracer if you prefer that method of debugging. Using Strict Warnings in Mozilla will provide help with finding potential bugs in the console. Opera's JavaScript Console is excellent for tracking event bugs and is closing in on the Mozilla JavaScript Console for function debugging. When it comes to Internet Explorer, you can download the Windows Scripting Debugger, which is not as advanced as Venkman, and is not as good at tracing errors, but doubles as an editor. Mozilla also has a few other tools in the form of the DOM Inspector and various add-ons, and such as Live HTTP Headers, which can be useful for tracking server-related errors, such as files being sent using the wrong filetype, or file name resolution problems.

    As for the onload event, the charm of it is that you know all the script files are downloaded and parsed before it triggers.

    As for the bug, you don't realise what I mean. If you send a value which you did not intend a function to be able to parse, the error will still occur within the function instead of at the function call, which is the place things wen't wrong. Because of that, you can debug functions all you like, but since JavaScript is dynamically typed you must rely on not only the functions themselves to be robust, but also the input to always be right, as well as the object the function is bound to (if any), and the external functions and variables the function calls upon.




    (And this is a perfect example of a thread placed in the wrong forum. It should be placed in the JavaScript Programming forum. Hopefull a mod will come by and correct that.)
    Last edited by liorean; 01-13-2004 at 02:52 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New Coder
    Join Date
    Nov 2003
    Location
    Sasolburg, South Africa
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks liorean,

    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.

    Regards
    Dieter


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •