473,383 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

"Live" DOM NodeList: rationale?

Given the html at the end of this message, I see how
a DOM NodeList exhibits its "live" behavior; that is,
adding elements to a document can change any NodeList
variables, when there's *no* code that refers to them.

I suppose I can imagine cases where this idea would be
useful, but I can imagine many more where I'd like to
get a *static* list of, e.g., the <p> elements in my
document, and do something with them, regardless of any
I might add while I'm working on the list.

Can anyone explain the rationale behind this decision?

hj

---
Sample code exhibiting this behavior:

<html><head><title>Live Collection Demo</title></head><body>
<h1>Live Collection Demo</h1>
<script type="text/javascript">
function dumplist(plist) {
var s = plist.length + ' elements:\r\n ';
for (var i=0; i<plist.length; i++)
s += "plist[" + i + "] id='" + plist[i].getAttribute('id') + "'\r\n ";
return s;
}
function add_para() {
var plist,newp,lastp,desc,description;

plist = document.getElementsByTagName('p');
lastp = document.getElementById('last');
description = ' Initially: ' + dumplist(plist);
newp = document.createElement('p');
newp.appendChild(document.createTextNode('A middle paragraph.'));
newp.setAttribute('id','middle');
lastp.parentNode.insertBefore(newp,lastp);
description += '\r\n After adding: ' + dumplist(plist);
description += '\r\n\r\n The element referenced at plist[1] changed ' +
'with no reference to plist. It\'s "live".'
desc = document.createElement('pre');
desc.style.whitespace = 'pre';
desc.appendChild(document.createTextNode(descripti on));
document.body.appendChild(desc);
}
</script>
<p id="first">There are initially two paragraphs in this document.
This one's id is "first".
</p><p id="last">This paragraph's id is "last".</p>
<form action="" onsubmit="return false">
<button onclick="add_para()">Add a paragraph</button>
</form></body></html>
Jul 23 '05 #1
2 2747
Howard Jess wrote:
Given the html at the end of this message, I see how
a DOM NodeList exhibits its "live" behavior; that is,
adding elements to a document can change any NodeList
variables, when there's *no* code that refers to them.

I suppose I can imagine cases where this idea would be
useful, but I can imagine many more where I'd like to
get a *static* list of, e.g., the <p> elements in my
document, and do something with them, regardless of any
I might add while I'm working on the list.

Can anyone explain the rationale behind this decision?

hj


I'd guess the rationale is that they didn't want to create two node types to
represent "real" nodes and those that were added programmatically. Ultimately,
there is no difference between them, so there is no need to treat them differently.

It's not hard to keep track of nodes you insert programmatically anyway. When you
add elements to the DOM programmatically, set the ID to something like "jsMyId".
Then you can do:

var paragraphs = document.getElementsByTagName("p");
if (p) {
for (var i = 0; i < p.length; i++) {
if (p[i].id.indexOf("js") != 1) {
// it was part of the downloaded HTML
} else {
// it was added by JavaScript
}
}
}

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
Howard Jess <ho****@at.dhitechnologies.dot.com> writes:
I suppose I can imagine cases where this idea would be
useful, but I can imagine many more where I'd like to
get a *static* list of, e.g., the <p> elements in my
document, and do something with them, regardless of any
I might add while I'm working on the list.
It's easier to go from a dynamic list to a static than
the other way :)
---
function arrayOfNodeList(nodeList) {
return Array.prototype.slice.call(nodeList, 0, nodeList.length);
}
---
which oddly enough doesn't work in Opera (well, there's host objects
involved, so all bet's are off). Or the longer:
---
function arrayOfNodeList(nodeList) {
var arr = new Array();
for (var i=0; i<nodeList.length;i++) {
arr[i] = nodeList[i];
}
return arr;
}
---
Can anyone explain the rationale behind this decision?


Only the ones who made it. But my guess is the above.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3

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

Similar topics

1
by: could ildg | last post by:
I know that ":" can do slice works. But I saw "::" today and it puzzled me much, I can't find it in the python doc, so I raise this question here. The code is as below:...
8
by: chris yoker via DotNetMonster.com | last post by:
hiya, I succesfully return a "nodeList" thru the "xmlDoc.SelectNodes" method. This nodeList is taken from a repetitive, uniform xml doc. I can successfully append a child node at the correct...
38
by: Jack Klein | last post by:
Many people, including me, like having hard copy books at hand, along with electronic editions. When I first heard of this book several months ago, I pre-ordered a copy. It arrived from...
0
by: warenbe | last post by:
hi i've got a problem first sorry fot my bad english! i'm writing a soft wich send a file on a server using webclient.UploadFileAsync. it work fine but only with small files... in fact it...
1
by: johnny | last post by:
Is there any open source "live web chat support" program or script out there? Any you can recommend?
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
0
by: Carl J. Van Arsdall | last post by:
Hey python, I want to create a process that would "expire" if it didn't complete in a set amount of time. I don't seem to see any timeout values to pass os.system - does anyone know of a good...
0
by: John Krukoff | last post by:
On Fri, 2008-08-15 at 10:00 -0700, Carl J. Van Arsdall wrote: Well, if you're on unix, the signal module is probably the easiest method: Python 2.5.2 (r252:60911, Jul 31 2008, 15:38:58) on...
9
by: jrod11 | last post by:
I am working on a new project for a company intranet portal. it is based on the dropthings program found here: www.codeplex.com/dropthings I have everything working the way i want and I am ready...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.