Finding last node

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JonQuark

    #1

    Finding last node

    Hi,

    I suspect this is a common question but I've read the FAQ and searched
    the archives and couldn't find it. In a script embedded in the body of
    the page I want to quickly find the last node that was created and
    appendChild nodes to it. I can't just use document.body.a ppendChild as
    the last node is likely to be a grandchild or great-grandchild of body.
    I know that I could document.write an element with an id and then
    getElementById but this slow and clunky, there must be a faster way,
    document.write( ) knows where to put its output.

    In case I've not made myself clear:
    [...inside an HTML document...]
    Some text
    <script>text=do cument.createTe xtNode("DOM");</script>
    <table>
    <tr>
    <td>elem 1</td>
    <td>elem
    2<script>docume nt.write("locat ion");document. body.appendChil d(text);</script></td>
    </tr>
    <tr>
    <td>elem 3</td>
    <td>elem 4</td>
    </tr>
    </table>
    [...end of snippet...]
    I'd like to replace document.body with something that would make the
    text DOM appear after location rather than after the table and not
    depend on the containing td having an id that I getElementById for.

    Any help appreciated,

    Jon.

  • Martin Honnen

    #2
    Re: Finding last node



    JonQuark wrote:

    [color=blue]
    > I suspect this is a common question but I've read the FAQ and searched
    > the archives and couldn't find it. In a script embedded in the body of
    > the page I want to quickly find the last node that was created and
    > appendChild nodes to it. I can't just use document.body.a ppendChild as
    > the last node is likely to be a grandchild or great-grandchild of body.
    > I know that I could document.write an element with an id and then
    > getElementById but this slow and clunky, there must be a faster way,
    > document.write( ) knows where to put its output.
    >
    > In case I've not made myself clear:
    > [...inside an HTML document...]
    > Some text
    > <script>text=do cument.createTe xtNode("DOM");</script>
    > <table>
    > <tr>
    > <td>elem 1</td>
    > <td>elem
    > 2<script>docume nt.write("locat ion");document. body.appendChil d(text);</script></td>
    > </tr>
    > <tr>
    > <td>elem 3</td>
    > <td>elem 4</td>
    > </tr>
    > </table>
    > [...end of snippet...]
    > I'd like to replace document.body with something that would make the
    > text DOM appear after location rather than after the table and not
    > depend on the containing td having an id that I getElementById for.[/color]

    It is a bit tricky, if you have a script that is executed during page
    load and wants to insert content during page load then if the browser
    plays nice you could assume that document.getEle mentsByTagName( 'script')
    can be used to find the current <script> element as the last script
    element in that collection and then you can use appendChild on its
    parentNode to insert content at the correct position.

    I have made the following test case:
    <http://home.arcor.de/martin.honnen/mozillaBugs/domInsertionDur ingPageLoad/docWriteMixAppe ndChild.html>
    It shows that mixing document.write of pure text with appendChild leads
    to inconsistent behavior in different browsers (tested with IE 6,
    Netscape 7.2, Opera 7.50) so if you really think you need to use DOM
    Core stuff like appendChild during page load then do not mix it with
    document.write of pure text, the order of dynamically inserted content
    would then differ depending on the browser.

    But of course there could be other browsers where even the strategy of
    appending to the parentNode of the last script element doesn't yield the
    desired behavior, perhaps some Safari or Konqueror users can report what
    the test case does for them.


    --

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

    Comment

    • Fred Oz

      #3
      Re: Finding last node

      Martin Honnen wrote:
      [...][color=blue]
      > But of course there could be other browsers where even the strategy of
      > appending to the parentNode of the last script element doesn't yield the
      > desired behavior, perhaps some Safari or Konqueror users can report what
      > the test case does for them.
      >
      >[/color]

      Count sequence for Safari 1.0.3 (Mac OS 10.2.8):

      1,2 ... 12,14,13,15 ... 25,27,26,28 .


      Hope that helps.

      --
      Fred

      Comment

      • Martin Honnen

        #4
        Re: Finding last node



        Fred Oz wrote:
        [color=blue]
        > Martin Honnen wrote:
        >[color=green]
        >> perhaps some Safari or Konqueror users can
        >> report what the test case does for them.
        >>[/color]
        >
        > Count sequence for Safari 1.0.3 (Mac OS 10.2.8):
        >
        > 1,2 ... 12,14,13,15 ... 25,27,26,28 .[/color]

        Thanks, so Safari exhibits the same problematic combination,
        document.write of pure text mixed with appendChild seems to change the
        sequence.


        --

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

        Comment

        • Jon Levell

          #5
          Re: Finding last node

          Hi Martin,

          Thank you very much for your informative reply. I had considered
          getElementByTag Name but the application I'm working on is already
          pushing Javascript quite hard and such a script would be embedded
          literally hundreds of times inside a single big page. Building the array
          of scripts in each script will be a very slow process. I was hoping that
          there was going to be a variable perhaps called something like
          document.curren tNode that is updated as the page is created.

          Thanks again for your help.

          Jon.


          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Evertjan.

            #6
            Re: Finding last node

            Jon Levell wrote on 13 feb 2005 in comp.lang.javas cript:
            [color=blue]
            > Thank you very much for your informative reply. I had considered
            > getElementByTag Name but the application I'm working on is already
            > pushing Javascript quite ...[/color]

            Please either answer with email, or if you answer on usenet, quote the
            essentials of the posting you are answering on.

            Netiquette is sensible.

            --
            Evertjan.
            The Netherlands.
            (Replace all crosses with dots in my emailaddress)

            Comment

            • Martin Honnen

              #7
              Re: Finding last node



              Jon Levell wrote:
              [color=blue]
              > the application I'm working on is already
              > pushing Javascript quite hard and such a script would be embedded
              > literally hundreds of times inside a single big page. Building the array
              > of scripts in each script will be a very slow process.[/color]

              Well with the DOM collections are live so as the example page
              demonstrates you only need to call
              document.getEle mentsByTagName( 'script') once, the DOM implementation
              then takes care of updating the collection automatically without any
              need to call again. But of course that automatic updating will also take
              time if you have hundreds of script elements.
              [color=blue]
              > I was hoping that
              > there was going to be a variable perhaps called something like
              > document.curren tNode that is updated as the page is created.[/color]

              I don't think the W3C DOM has anything like that as it is not really
              concerned with script manipulating the DOM tree while it is built.
              And I don't know of any browser exposing such a property.

              --

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

              Comment

              Working...