DOM script questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #1

    DOM script questions

    (Sorry for the duplicate post - I was going to change the subject
    before posting and forgot...)

    I'm trying to create script that can dynamically execute script from a
    given source and then call a callback function when complete. I have
    the following...

    function bar() {
    alert( 'bar' );
    }
    function foo() {
    var s=document.getE lementById( 'dynamicScriptE lement' );
    if( !s ) {
    s=document.crea teElement( 'script' );
    s.type='text/javascript';
    s.id='dynamicSc riptElement';
    document.docume ntElement.child Nodes[1].appendChild( s );
    }
    s.src='test.js' ;
    bar();
    }

    1) Is the script in test.js guaranteed to be executed before bar() is
    called?
    2) foo() assumes that the <html> element has a <head> and <body> -
    presumably that's reasonable, right?
    3) Any other comments would be appreciated!

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Lasse Reichstein Nielsen

    #2
    Re: DOM script questions

    Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
    [color=blue]
    > 1) Is the script in test.js guaranteed to be executed before bar() is
    > called?[/color]

    There is no standard saying what must happen when you load scripts
    after the page has finished loading (and I'm not sure there are
    any for before either), so guarantees are hard to make.

    My guess is that you can be almost certain that the script has *not*
    been loaded.
    [color=blue]
    > 2) foo() assumes that the <html> element has a <head> and <body> -
    > presumably that's reasonable, right?[/color]

    Not necessarily. If the HTML page is a frameset page, it need not have
    a body. Otherwise, it's a fairly safe bet, if the HTML page is valid.
    [color=blue]
    > 3) Any other comments would be appreciated![/color]

    Not all browsers allow loading scripts later. Opera started supporting
    it somewhere in the 7 series.

    Why:
    document.docume ntElement.child Nodes[1].appendChild( s );
    (which only works in IE in standards mode, not quirks) and not just
    document.body.a ppendChild(s)
    Don't assume that childNodes[1] is head or body, it can be a text
    node (which would make appendChild fail).

    The only way to be certain that a script has loaded is to have that
    script call the callback itself.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Martin Honnen

      #3
      Re: DOM script questions



      Christopher Benson-Manica wrote:

      [color=blue]
      > I'm trying to create script that can dynamically execute script from a
      > given source and then call a callback function when complete. I have
      > the following...
      >
      > function bar() {
      > alert( 'bar' );
      > }
      > function foo() {
      > var s=document.getE lementById( 'dynamicScriptE lement' );
      > if( !s ) {
      > s=document.crea teElement( 'script' );
      > s.type='text/javascript';
      > s.id='dynamicSc riptElement';
      > document.docume ntElement.child Nodes[1].appendChild( s );
      > }
      > s.src='test.js' ;
      > bar();
      > }
      >
      > 1) Is the script in test.js guaranteed to be executed before bar() is
      > called?[/color]

      No, browsers can and in reality do load the script file asynchronously
      so you set src and that triggers loading the script (or for instance
      loading an image if you had new Image().src = 'whatever.gif') but then
      the script exection continues meaning the bar(); call is executed.
      So you need an onload or onreadystatecha nge event handler on the script
      element, Mozilla and IE should support that, not sure about other browsers.
      [color=blue]
      > 2) foo() assumes that the <html> element has a <head> and <body> -
      > presumably that's reasonable, right?[/color]

      So
      document.docume ntElement.child Nodes[1]
      is assumed to be the <body> element? That is not reasonable. Depending
      on white space text node parsing the number of child nodes in an element
      differs between the different DOM implementations browsers have.

      If you want to script the <body> element use document.body (as long as
      you are dealing with text/html content at least) but of course
      document.body is only an object once the opening <body> tag has been
      parsed thus if you intended to call foo earlier you were in trouble.


      --

      Martin Honnen
      http://JavaScript.FAQTs.com/

      Comment

      • Christopher Benson-Manica

        #4
        Re: DOM script questions

        Lasse Reichstein Nielsen <lrn@hotpop.com > spoke thus:
        [color=blue]
        > My guess is that you can be almost certain that the script has *not*
        > been loaded.[/color]

        I suppose, based on Martin's response, that you are correct - I just
        had something simple in test.js to test, so I suppose that's why it
        appeared to execute before bar().

        [color=blue]
        > Not all browsers allow loading scripts later. Opera started supporting
        > it somewhere in the 7 series.[/color]

        Do you know whether IE 5.0 supports this?
        [color=blue]
        > (which only works in IE in standards mode, not quirks)[/color]

        Sounds like no :(
        [color=blue]
        > The only way to be certain that a script has loaded is to have that
        > script call the callback itself.[/color]

        Well that's unfortunate. Then again this whole situation is
        unfortunate :)

        --
        Christopher Benson-Manica | I *should* know what I'm talking about - if I
        ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        Working...