SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member Randy_A's Avatar
    Join Date
    Mar 2004
    Sugar Land, Tx
    0 Post(s)
    0 Thread(s)

    Existance dependent code execution

    not a timing issue or which comes first issue, just an existance issue.

    <script type='text/javascript' src="MakeElements.js"></script> 
    <script type='text/javascript'> 
    function CallMakeElements()
      if (!MakeElements) { alert("missed this time,  MakeElements.js not parsed yet"); }
    // OR this
    if ((MakeElements) && (!document.body)) {alert("missed this time,  no document.body yet"); }
    <body onload='CallMakeElements()'>
    Until both exist (the function MakeElements() and the document.body)
    any attempt to execute will fail.
    Existance at any given *time* is depenant upon many things:
    • server responce time
    • internet traffic
    • MakeElements.js file name misspelled or missing
    • errors within MakeElements.js
    • no browser include support
    • <body> parsed before MakeElements.js
    • MakeElements.js parsed before <body>
    • etc., etc...

    I was not sure how to search for this issue in previously posted msgs.
    So I posted this.

    I was curious how often this has been a real problem for others?
    (not the 'chicken OR the egg' but the 'chicken AND the egg')

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    0 Post(s)
    0 Thread(s)
    Not exactly sure what your question is. The synchronicity problem is non-existent: JavaScripts are loaded in series (unlike, say, images) for just that reason - the necessity of keeping absolute control over the availability of program routines & data. The browser won't parse the embedded part of your code until the 'external' file is downloaded and processed; presumably, if your host is down and can't serve the .js file, it won't be sending the containing .html file either. The body object is instantiated as soon as the opening <body> tag is parsed - reference it before, error, anytime after, OK. Lexical errors are self-explanatory (careful!). Never heard of a 'browser include' - did you mean support for <script src="...">? Pretty much a non-issue. The principal timimg issues involve the availability of browser objects, as well as some individual (browser) quirks in making those objects scriptable immediately upon creation. The basic sequencing of script elements is not really (imo) an 'issue' as such, just requires knowing how to write scripts.
    if (typeof MakeElements != 'undefined') {.....
    See also try/catch.
    ::: certified wild guess :::


Posting Permissions

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