473,792 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Document nodes returning null

7 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. var ProcNodes=function(node,fn)
  2. {
  3.     var n=0,
  4.         i=0,
  5.         nodes=node.childNodes,
  6.         length=nodes.length;
  7.     for(i=0;i<length;i++)
  8.     {
  9.         var node=nodes[i];
  10.         fn.call(node);
  11.         if(node.childNodes.length>0)
  12.             n+=ProcNodes(fn,node);
  13.         n++;
  14.     }
  15.     return n;
  16. }
thx in advance,
Yoann Arseneau
Nov 20 '07 #1
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:

Expand|Select|Wrap|Line Numbers
  1. var ProcNodes = function(node, fn) {
  2.     var nodes  = node.childNodes;
  3.     var length = nodes.length;
  4.  
  5.     for (var i = 0 ; i < length; i++) {
  6.         var node = nodes[i];
  7.  
  8.         fn.call(node);
  9.  
  10.         if (node.childNodes.length > 0) {
  11.             ProcNodes(node, fn);
  12.         }
  13.      }   
  14. }
  15.  
  16. ProcNodes(document.getElementsByTagName('body')[0], function() {
  17.     alert(this.nodeType);
  18. });
  19.  
we don't need the n and i fixed the order of params in the recursion call

kind regards
Nov 20 '07 #2
drdeath89
7 New Member
heh... well im kinda embarrassed :\

thx for your help XD
Nov 20 '07 #3
mrhoo
428 Contributor
Excuse this- I missed gits post.

Expand|Select|Wrap|Line Numbers
  1. nodecall= function(node,fun){
  2.     var n= [],tem;    
  3.     tem= node.firstChild;
  4.     while(tem){        
  5.         n.push(fun.call(tem));
  6.         if(tem.hasChildNodes()) n.push(arguments.callee(tem,fun));
  7.         tem= tem.nextSibling;        
  8.     }
  9.     return n;
  10. }
Expand|Select|Wrap|Line Numbers
  1. function ahoy(){
  2.     return this.nodeName;
  3. }
var A=nodecall(docu ment.body,ahoy) ;
alert(A.join('\ n\n'));
Nov 20 '07 #4
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
Nov 20 '07 #5
drdeath89
7 New Member
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 :\
Nov 20 '07 #6
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?
Nov 20 '07 #7
drdeath89
7 New Member
Expand|Select|Wrap|Line Numbers
  1. if(node==null)
  2. {
  3.     alert("ParseNode: null node");
  4.     return;
  5. }
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
Nov 20 '07 #8
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]' ...
Nov 20 '07 #9
drdeath89
7 New Member
Well this is prety wierd... When i changed the statement on line 7 my script suddenly started acting exactly like expected.

Expand|Select|Wrap|Line Numbers
  1. var ProcNodes=function(node,fn)
  2. {
  3.     var nodes=node.childNodes,length=nodes.length,n=0;
  4.     for(var i=0;i<length;i++)
  5.     {
  6.         var node=nodes[i];
  7.         if(!fn(node))// was previously fn.call(node)
  8.             return-n;
  9.         if(node.childNodes.length>0)
  10.             n+=ProcNodes(node,fn);
  11.         n++;
  12.     }
  13.     return n;
  14. }
(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?

Expand|Select|Wrap|Line Numbers
  1. someFunction("parameter")
  2. someFunction.call("parameter")
<Edit>And yes, it does alert text node objects for whitespace nodes with the new code
Nov 21 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1384
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>
12
10177
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.
136
9460
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,...
4
5018
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...
2
7442
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,
1
1906
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);
2
7855
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.
7
2408
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; }
2
2668
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...
0
9670
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
10430
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
10211
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...
1
10159
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,...
0
10000
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
9033
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
7538
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
5436
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...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.