Im writing some Javascript code that requires me to loop all child nodes and sub child nodes of a certain parent node and execute a function with the child node in question.
I decided to write a recursive function with said functionality, with a node and a function as parameters, but it simply wont work.
My problem is that whenever i pass a node to this function parameter it is always, regardless of what i change, equal to null. I even modified my custom passed function to ignore null nodes, and now it throw a null variable error every time my function recurses.
(exact error: node.childNodes has no properties)
To me it looks like a problem with these infamous 'closures' i keep hearing about. I understand the concept, but I dont know exactly how they work in this context due to a great lack of experience :(.
I know there are many sample codes out there that can do the job, but I want to know why THIS function, which looks perfectly logical to me, doesnt behave as i would expect it too.
I read a few posts here and there relating to this, and I modified my code accordingly, but i cant seem to figure out just what is causing my problem. - var ProcNodes=function(node,fn)
-
{
-
var n=0,
-
i=0,
-
nodes=node.childNodes,
-
length=nodes.length;
-
for(i=0;i<length;i++)
-
{
-
var node=nodes[i];
-
fn.call(node);
-
if(node.childNodes.length>0)
-
n+=ProcNodes(fn,node);
-
n++;
-
}
-
return n;
-
}
thx in advance,
Yoann Arseneau
14 2021 gits 5,390
Recognized Expert Moderator Expert
hi ...
welcome to TSDN ...
i fixed your code ... have a close look at the example ... i tested it with google.com in firebug and it worked: -
var ProcNodes = function(node, fn) {
-
var nodes = node.childNodes;
-
var length = nodes.length;
-
-
for (var i = 0 ; i < length; i++) {
-
var node = nodes[i];
-
-
fn.call(node);
-
-
if (node.childNodes.length > 0) {
-
ProcNodes(node, fn);
-
}
-
}
-
}
-
-
ProcNodes(document.getElementsByTagName('body')[0], function() {
-
alert(this.nodeType);
-
});
-
we don't need the n and i fixed the order of params in the recursion call
kind regards
heh... well im kinda embarrassed :\
thx for your help XD
Excuse this- I missed gits post. - nodecall= function(node,fun){
-
var n= [],tem;
-
tem= node.firstChild;
-
while(tem){
-
n.push(fun.call(tem));
-
if(tem.hasChildNodes()) n.push(arguments.callee(tem,fun));
-
tem= tem.nextSibling;
-
}
-
return n;
-
}
- function ahoy(){
-
return this.nodeName;
-
}
var A=nodecall(docu ment.body,ahoy) ;
alert(A.join('\ n\n'));
gits 5,390
Recognized Expert Moderator Expert
heh... well im kinda embarrassed :\
thx for your help XD
hi ...
no problem ... post back to the forum anytime you have more questions ...
kind regards
Im still getting some null nodes, but i think i just figured it out.
From what i can tell, firefox (browser im currently testing on) returns whitespace textnodes as null objects.
Can someone confirm this so i can finally stop worrying about this insignificant part of my app :\
gits 5,390
Recognized Expert Moderator Expert
it returns them as textnodes ... nodeType == 3 or nodeName == '#text' ... so with that you may handle them ... could tell where you get a null-value?
- if(node==null)
-
{
-
alert("ParseNode: null node");
-
return;
-
}
this message alerts several times during my loop, it is the first statement inside my calling function (fn parameter)
<EDIT>
the node variable is the one directly passed from the recursive function
gits 5,390
Recognized Expert Moderator Expert
hmmm ... that is strange ... could you post your html ... i think in case of a text node it should alert a '[object Text]' ...
Well this is prety wierd... When i changed the statement on line 7 my script suddenly started acting exactly like expected. - var ProcNodes=function(node,fn)
-
{
-
var nodes=node.childNodes,length=nodes.length,n=0;
-
for(var i=0;i<length;i++)
-
{
-
var node=nodes[i];
-
if(!fn(node))// was previously fn.call(node)
-
return-n;
-
if(node.childNodes.length>0)
-
n+=ProcNodes(node,fn);
-
n++;
-
}
-
return n;
-
}
(the n variable is there because i need it for debugging purposes)
Honestly, I only used the call method because I saw another script use it and I thought it looked cool. :P
I assumed it worked identical to normal calling syntax which appears not to be the case, atleast not on my firefox. Can anyone confirm if there is a difference between these two lines of code? - someFunction("parameter")
-
someFunction.call("parameter")
<Edit>And yes, it does alert text node objects for whitespace nodes with the new code
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Andy Fish |
last post by:
Say I am in an XSLT template with the context node set to a node from an
alternative document (i.e. read by the document() function). Is there any
way to select nodes from the original document again?
e.g.
<xsl:template match="foo">
<xsl:apply-templates select="document(doc.xml)" mode="otherdoc" />
</xsl:template>
|
by: Kepler |
last post by:
How do you get the height of the client browser in IE? Both
document.body.clientHeight and document.body.offsetHeight return the
height of the document. If the page is long and there's a vertical
scrollbar, you get the height of the entire document, screwing up any
chance of centering a window in the browser using these values.
Is there a way to get the height of the actual browser window and not
the entire page height?
Thanks.
|
by: Matt Kruse |
last post by:
http://www.JavascriptToolbox.com/bestpractices/
I started writing this up as a guide for some people who were looking for
general tips on how to do things the 'right way' with Javascript. Their code
was littered with document.all and eval, for example, and I wanted to create
a practical list of best practices that they could easily put to use.
The above URL is version 1.0 (draft) that resulted. IMO, it is not a
replacement for the FAQ,...
|
by: Jim |
last post by:
Hi
I'm looking to take an existing XML document, query for certain nodes, and 'recreate' the document with just the relevant nodes. I'm currently using XPath - I have established the pattern that returns the required child nodes from the document, but am struggling to find a good statergy for recreating the file
Here is an excert of my XML file
<resource_data><planets><planet...
|
by: Björn Langhof |
last post by:
Hello.
I want to evaluate a XPath-Expression only on a subtree of the whole
xml-document.
1. I select a node of the XML-document
2. Then a want to select specific nodes below the node chosen in 1.
I thought the parameter contextNode in
var xpathResult = document.evaluate(xpathExpression, contextNode,
| |
by: shellon |
last post by:
Hi all:
when I use XPather(a firefox extension) to evaluate the expression:
"/html/body/table/tbody/tr/td/table/tbody/tr/td/div/ul/li"
it tells me there are 7 matching Nodes.
but when I use the following code to do the same thing:
nodes =
document.evaluate("/html/body/table/tbody/tr/td/table/tbody/tr/td/div/ul/li",
document, null,XPathResult. ORDERED_NODE_SNAPSHOT_TYPE , null);
|
by: Andy |
last post by:
Hi, I have an XML document that uses namespaces (it is from a Word 2007
file). I want to retrieve all the "t" elements that belong to the
"w" namespace (<w:t>) using XPath from VB.NET 2003 (.NET framework
1.1).
I've successfully loaded the document into a XmlDocument DOM parser
(I can dump the contents using OuterXML).
And, I've created a XmlNamespaceManager and assigned it the "w"
namespace.
|
by: martinfrompi |
last post by:
I have a container of XmlNodes. Some of them have been removed from
the document, some haven't. Is there an easy way to tell which are
which?
I suppose
bool IsPartOfTree(XmlNode node)
{
while (node.ParentNode != null) { node = ParentNode; }
return node == node.OwnerDocument;
}
|
by: nicky123 |
last post by:
Hi everyone,
This is a brief description that I have provided for parsing & displaying an XML document using DOM API. Please feel free to post your own comments & views regarding this discussion. Thank you.
The first step of parsing an XML document is to import the DOM API related classes such as :-
java.io.* which contains all the interfaces to perform an I/O operation.
org.xml.sax.* which contains all the interfaces...
|
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...
|
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...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |