SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    embedded works - external doesn't

    I'm trying to learn JS and have run into a problem I can't get around with embedded vs an external file. The link is at http://www.peachtreewebs.com/js/protos.html and has the same code embedded and also in an external file. The code is the same - but the alert box results for the external file are not what I expect. Any suggestions would be appreciated.

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,345
    Mentioned
    192 Post(s)
    Tagged
    5 Thread(s)
    First thing to bear in mind is the process of Page Loading.
    With the external reference in the head, the entire contents of the js file gets loaded (and executed) at that point.
    Your inline Javascript, also is executed as it is encountered; which is after the entire page has loaded.
    I can't see the external file, but I assume you are making the same "getElementBy..." call. The element does not exist until the page has loaded.

    In order to see this (as a great learning experience), wrap your code (in the external file) in a try...catch block. You will see if an error has been raised and can then dig into the error object to get the details.

    Good luck. Have fun. Javascript is a great language. I have been working with it since it was first introduced (it had a different name). Javascript has matured very nicely and can provide a lot of great learning that is applicable to other OOP languages.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - I had a good idea it had to do with the page load sequence. I've tried using the defer attribute with no success - so I'll try the try...catch.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use the onload event of the window/document to trigger the beginning of script execution?

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had not got to that page yet


Bookmarks

Posting Permissions

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