473,667 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Jul 23 '05 #1
6 1804


JonQuark wrote:

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.


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/
Jul 23 '05 #2
Martin Honnen wrote:
[...]
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.


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
Jul 23 '05 #3


Fred Oz wrote:
Martin Honnen wrote:
perhaps some Safari or Konqueror users can
report what the test case does for them.


Count sequence for Safari 1.0.3 (Mac OS 10.2.8):

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


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/
Jul 23 '05 #4
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!
Jul 23 '05 #5
Jon Levell wrote on 13 feb 2005 in comp.lang.javas cript:
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 ...


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)

Jul 23 '05 #6


Jon Levell wrote:
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.
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.
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.


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/
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1736
by: Jamie Green | last post by:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am trying to query the following document <Root> <Child>Name</Child> <Child>John</Child> <Child>Smith</Child> <Child>23</Child> <Child>Name</Child> <Child>Peter</Child>
4
6566
by: Victor Engmark | last post by:
When looking for a method to fetch unique elements and counting the number of occurences of each of them, I found quite a lot of gross examples of complex XSL. But after realizing the subtle difference between "." and "current()", I found a neat way of doing the same without keys or generate-id(): <xsl:template match="/"> <!-- Selects all "new" elements --> <xsl:for-each select="//Name"> <!-- Display the element -->
0
1380
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If indexing was used, I could do something like child = hole * 2 + 1; but since only thing the function accepts are random access Iterators, how do I calculate the Iterator to the child node? template <typename Iterator, typename Functor> void...
7
2154
by: Scott W Gifford | last post by:
Hello, I'm considering using XML to represent a stream of location information, and XPath to do queries against it. I've got most of it figured out (at least on paper), but I can't figure out how to create an XPath statement asking for the "last node with a value less than" a given value. I need this to be able to ask "Where was Scott at 11:00 yesterday", which should find the last sighting of Scott before or at 11:00
6
7070
by: Jay Bienvenu | last post by:
I have the following Visual Basic .NET code in an ASP.NET project: ' ... Dim xdStructure As XmlDocument Dim xnStructureRoot As XmlNode xdStructure = New XmlDocument xdStructure.Load(Server.MapPath("~/xml/Structure.xml"))
2
4730
by: TT (Tom Tempelaere) | last post by:
Hey there, I'm having trouble finding the right XPath expression. The XML file I'm parsing contains an element Heads which contains sereveral Head elements. These Head elements have several Row sub-elements. Heads ->(0..*)Head -> ...subelements... -> (0..*)Row The problem I have is that I find the wrong Row elements, ie I always find the FIRST Row element inside the complete Xml document. This is probably
2
2292
by: Lior | last post by:
Hi, I have an ASP.NET website that crashes under heavy load. I use a SQL Server DB. I get around 5500 hits per day. I keep getting the timeout expieried connection pool error. Sometimes it even throws and error about a DataReader connection being already open even though I only use Data Sets in my code. Please take a look and see if you can find my leak because I'm going nuts here and am losing hope... I keep blaming the server and the...
6
14194
by: meh | last post by:
I can figure out the total number of nodes in a given tree but what I'd like to know is what is the Selected Nodes relationship to the entire tree i.e This is node n out of nnn nodes. In most of my database apps its common place to tell the user that they are working on record x of xxx records I'm trying to provide the same functionality using the tree control. TIA meh
2
1654
by: skrebbel | last post by:
Is there any faster/easier way of finding a child node of a certain DOMNode object than doing something like function getChildByTagname($curnode, $name) { foreach($curnode->childNodes as $node) { if($node->nodeName==$name) { return $node;
0
8458
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.