473,804 Members | 3,705 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
14 2022
gits
5,390 Recognized Expert Moderator Expert
hi ...

the following is a regular function call:

Expand|Select|Wrap|Line Numbers
  1. func(param);
  2.  
the call() method may receive more than one parameter ... where the first parameter is used as the scope that the function should be called in ... so when we use:

Expand|Select|Wrap|Line Numbers
  1. func.call(param, param1);
  2.  
then param is used for the scope and param1 as function-parameter ... but as far as i can see the code i fixed for you works as intended ... the fn alerts the correct nodeType for textnodes ... because the this is the node-context itself in the case of passing the node to the function.call(n ode) ... so to check that could you please post your html that produces the error? and/or the fn-code?

kind regards
Nov 21 '07 #11
drdeath89
7 New Member
While you did answer my question as to how the call function works, and i tested it to assure i understood, this definition explains why it did not work.

when you say this:
where the first parameter is used as the scope that the function should be called in
That means that when i was calling the function previously, i was sending the node as the context, and not as a parameter for the function body. For my code to work this way, i need to refer to the this object rather than the node parameter i was trying to access.
Nov 21 '07 #12
gits
5,390 Recognized Expert Moderator Expert
While you did answer my question as to how the call function works, and i tested it to assure i understood, this definition explains why it did not work.

when you say this:


That means that when i was calling the function previously, i was sending the node as the context, and not as a parameter for the function body. For my code to work this way, i need to refer to the this object rather than the node parameter i was trying to access.
exactly ;)

kind regards
Nov 21 '07 #13
drdeath89
7 New Member
this will teach me to use random functions without knowing what they do :P

this has been very instructive, thx very much :)
Nov 21 '07 #14
gits
5,390 Recognized Expert Moderator Expert
glad to hear that :) ... post back to the forum anytime you would have more questions ...

kind regards
Nov 22 '07 #15

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
10179
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
9463
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
5019
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
7456
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
1907
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
9705
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
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10074
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
9138
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
7613
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
6847
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();...
0
5516
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.