Timing of JavaScript blocks

Discussion in 'Javascript' started by DrKen, Jun 22, 2011.

  1. DrKen

    DrKen Guest

    Sorry to be asking so many questions (and no, I don't expect anyone to
    go back and look at what I wrote before--that wasn't what I was
    saying) but I'm going beyond what my big, fat Beginning JavaScript
    book says.

    I am required to add a script block just before the closing </body>
    tag. This small script needs to be able to use the value of a form
    field that is hidden (and gets its value through a special tag from
    the database on the fly). I'm trying to figure out if the hidden
    value will be available before this script block, that is all CDATA is
    executed or if I need to do something to make sure the hidden value is
    set first and then the block at the bottom of the HTML is executed.
    Since, as I learned in a previous answer from the group, the script
    blocks are executed in the order they occur (apart from event handlers
    like onChange calling functions), I'm hoping it is safe to assume a
    script block at the bottom won't be processed until after the page is
    loaded and the hidden fields are filled in. Is that a correct
    assumption? Thanks.

    Ken
     
    DrKen, Jun 22, 2011
    #1
    1. Advertisements

  2. DrKen

    Tim Streater Guest

    I wouldn't make assumptions like that. You should use something like:

    <body onload="yourscript();">

    Then you'll know that your script doesn't run until the whole of the
    page is loaded [1]. It also then won't matter where yourscript() is
    located within the page.

    Timing is important with this stuff. I'd be inclined to put all code
    into functions except for declaring globals (if you have any), and
    initialise those in your onload function. Then all your code is executed
    when events fire and your donkeymentation should describe when that is.

    [1] IIRC, there are some wrinkles about what "loaded" means, e.g. are
    all images rendered before this event fires ...
     
    Tim Streater, Jun 22, 2011
    #2
    1. Advertisements

  3. DrKen

    DrKen Guest

    Thanks Tim. In further experimenting, I have a further question about
    onload() vs. script parsing.

    I am required to add the following script block to my page and am
    supposed to put it as close to the bottom of the page as possible,
    like just before the </body> tag.

    <script type="text/javascript">
    //<![CDATA[
    s.channel="Admissions";
    s.pageName="Applications: Graduate: Start";
    s.hier1="applications,graduate";
    alert("omniIdentifier: " + omniIdentifier);
    s.events="event8:GRAD"+ "TEST" + omniIdentifier;
    //]]>
    </script>

    I set the value of omniIdentifer in a function fired at onload time.
    The variable is defined near the top of the file:
    <script type="text/javascript">
    var omniIdentifier = null;
    </script>
    The function is defined after the </form> element:

    <script LANGUAGE = JavaScript>
    // <!--Code to support Omniture -->
    // Added for SSD-245
    function getOmniIdentifierOnLoad()
    {
    omniIdentifier = document.getElementById("omniEmail").value;
    return true;
    }
    </script>

    This code must run before the CDATA block I was given. onload doesn't
    seem to run until after all the script parsing and such has taken
    place. How can I, as it were, beat the execution of the JavaScript at
    the bottom of the page. Right now, my alert() shows that the value I
    want is null. Thanks.

    Ken
     
    DrKen, Jun 23, 2011
    #3
  4. DrKen

    Tim Streater Guest

    Why don't you make that code be a function. Then you control when it's
    executed:

    <script type="text/javascript">
    //<![CDATA[
    function andFinally ()
    {
    s.channel="Admissions";
    s.pageName="Applications: Graduate: Start";
    s.hier1="applications,graduate";
    alert("omniIdentifier: " + omniIdentifier);
    s.events="event8:GRAD"+ "TEST" + omniIdentifier;
    }
    //]]>
    </script>

    Then you've got :

    <body onload="yourscript();">

    and finally:

    function yourscript ()
    {
    getOmniIdentifierOnLoad();
    andFinally ();
    }

    That way it's OK if for some reason your CDATA block needs to be
    physically near the end of the page.
     
    Tim Streater, Jun 23, 2011
    #4
  5. DrKen

    DrKen Guest

    I've been able to make this work, at least so far as I can tell
    running the page by itself in Firefox, using a function invoked from
    onload(). It looks to me like JS script blocks are executed, except
    of course functions, before the page is loaded, at least in FF. I
    don't know if this is true in all browsers but since I've put all the
    relevant code into a function, it seems to be okay. Thanks for the
    help.


    Ken
     
    DrKen, Jun 23, 2011
    #5
  6. In comp.lang.javascript message <[email protected]
    6g2000prn.googlegroups.com>, Wed, 22 Jun 2011 13:37:40, DrKen
    (1) Be aware that, when a page is reloaded in Firefox 3 after the user
    has altered an input control, and after the page source has been edited,
    then the value of the control is not lost (unless the value in the
    source was changed) - but may seem to be moved if the controls no longer
    appear in the same order. It can be very useful in testing, but might
    conceivably affect your situation.

    (2) On the whole, I agree with Tim.

    (3) You could, perhaps, add another hidden field, at the end of the page
    and set last, and not read the other hidden fields until the new field
    had been set to a magic value. Caveat (1).

    (Z) Different newsreaders display things differently. Do not assume
    that readers of an article will necessarily notice who wrote it, unless
    a sufficiently full real-type name is given in the body of the article,
    or you use a distinctive delimited sig.
     
    Dr J R Stockton, Jun 23, 2011
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.