473,472 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Too Much Recursion error in DOM Create & Remove Script

RMWChaos
137 New Member
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to create the child element. At the point where the script is supposed to call itself with the new properties to create the parent, it gives me the error and looks like it's looping the function continuously.

It seems that "id : attrib[1]" (line 192) is not being passed back into createDOM(). So, either a script cannot call itself, or I am missing something in this code. I tried setting attrib[1] to a global variable and passing the variable to the second instance of createDOM(), but that didn't work either. It should entirely skip this section of code in the second instance because !parentExist is false at that point. The only reason this section of code runs is if !parentExist and parent (attrib[1]) != 'body' are both true. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. /* **************************************** */
  2. /* **** Begin: DOM Add/Remove Functions *** */
  3. /* **************************************** */
  4.  
  5.  
  6.  
  7. /* **** Remove DOM Element Function ******* */
  8. function removeDOM(id)
  9.  
  10.     {
  11.  
  12.     var idExist = document.getElementById(id);
  13.  
  14.     /* **** Element does not exist, escape **** */
  15.     if (!idExist)
  16.  
  17.         {
  18.  
  19.         return;
  20.  
  21.         }
  22.  
  23.     /* **** Remove the element **************** */
  24.     else
  25.  
  26.         {
  27.  
  28.         removeDOM(idExist);
  29.  
  30.         };
  31.  
  32.     };
  33.  
  34.  
  35. /* **** Create DOM Elements Function ****** */
  36. function createDOM(attrib)
  37.  
  38.     {
  39.  
  40.     var defaultAttribs = 
  41.  
  42.         {
  43.  
  44.         'dom'      :     'div',
  45.  
  46.         'parent'   :     'body',
  47.  
  48.         'id'          :     null,
  49.  
  50.         'alt'         :     null,
  51.  
  52.         'src'        :     null,
  53.  
  54.         'method'  :     null,
  55.  
  56.         'type'      :     null,
  57.  
  58.         'value'     :     null,
  59.  
  60.         'size'      :     null,
  61.  
  62.         'name'    :     null,
  63.  
  64.         'text'      :     null,
  65.  
  66.         'onclick' :     null,
  67.  
  68.         'pSelect' :     null
  69.  
  70.         };
  71.  
  72.     for (var index in defaultAttribs)
  73.  
  74.         {
  75.  
  76.         if((typeof attrib[index] == 'undefined') && (attrib[1] != 'body'))
  77.  
  78.             {
  79.  
  80.             attrib[index] = defaultAttribs[index];
  81.  
  82.             };
  83.  
  84.         };
  85.  
  86.     var idExist = document.getElementById(attrib[2]);
  87.  
  88.     var parentExist = document.getElementById(attrib[1]);
  89.  
  90.     var createElement = document.createElement(attrib[0]);
  91.  
  92.     /* **** createElement to createTextNode *** */
  93.     if (attrib[0] == 'text')
  94.  
  95.         {
  96.  
  97.         createElement = document.createTextNode(attrib[10]);
  98.  
  99.         };
  100.  
  101.     /* **** Set Element id ******************** */
  102.     if (attrib[2] != null)
  103.  
  104.         {
  105.  
  106.         createElement.id = attrib[2];
  107.  
  108.         };
  109.  
  110.     /* **** Create script in document.head **** */
  111.     if (attrib[0] == 'script')
  112.  
  113.         {
  114.  
  115.         createElement.src = attrib[4];
  116.  
  117.         parentExist = document.getElementsByTagName('HEAD')[0];
  118.  
  119.         };
  120.  
  121.     /* **** Set parentExist to Body Element *** */
  122.     if (attrib[1] == ('body' || null))
  123.  
  124.         {
  125.  
  126.         /* **** W3C HTML DOM Level 1+ Standard **** */
  127.         if (document.getElementsByTagName)
  128.  
  129.             {
  130.  
  131.             parentExist = document.getElementsByTagName('BODY')[0];
  132.  
  133.             }
  134.  
  135.         /* **** Microsoft Implementation Shortcut * */
  136.         else if (document.body)
  137.  
  138.             {
  139.  
  140.             parentExist = document.body;
  141.  
  142.             }
  143.  
  144.         /* **** Netscape Navigator 4+ ************* */
  145.         else if (document.layers)
  146.  
  147.             {
  148.  
  149.             parentExist = document.tags['BODY'];
  150.  
  151.             };
  152.  
  153.         };
  154.  
  155.     /* **** Element is an Image with pSelect ** */
  156.     if ((attrib[0] == 'img') && attrib[12])
  157.  
  158.         {
  159.  
  160.         createElement.id = page[attrib[12]] + 'img';
  161.  
  162.         createElement.alt = attrib[3];
  163.  
  164.         createElement.title = attrib[3];
  165.  
  166.         createElement.src = attrib[4];
  167.  
  168.         createElement.onmouseover = 'mouseOver(' + attrib[12] + ')';
  169.  
  170.         createElement.onmouseout = 'mouseOut(' + attrib[12] + ')';
  171.  
  172.         createElement.onclick = onclick;
  173.  
  174.         };
  175.  
  176.     /* **** Element Exists & Not Adding Text ** */
  177.     if (idExist && (attrib[0] != 'text'))
  178.  
  179.         {
  180.  
  181.         return;
  182.  
  183.         };
  184.  
  185.     /* **** Create Parent or Target Element *** */
  186.     if (!parentExist)
  187.  
  188.         {
  189.  
  190.         createDOM({
  191.  
  192.             'id'     :     attrib[1]
  193.  
  194.             });
  195.  
  196.         };
  197.  
  198.     /* **** Create form element *************** */
  199.     if (attrib[0] == 'form')
  200.  
  201.         {
  202.  
  203.         createElement.name = attrib[2];
  204.  
  205.         createElement.action = attrib[4];
  206.  
  207.         createElement.method = attrib[5];
  208.  
  209.         };
  210.  
  211.     /* **** Create input elements for form **** */
  212.     if (attrib[0] == 'input')
  213.  
  214.         {
  215.  
  216.         createElement.form = attrib[1];
  217.  
  218.         createElement.type = attrib[6];
  219.  
  220.         /* **** Input is Submit or Reset button *** */
  221.         if (attrib[7] == ('Submit' || 'Reset'))
  222.  
  223.             {
  224.  
  225.             createElement.alt = attrib[3];
  226.  
  227.             /* **** Cross-browser Support for NN ****** */
  228.             createElement.title = attrib[3];
  229.  
  230.             createElement.src = attrib[4];
  231.  
  232.             createElement.value = attrib[7];
  233.  
  234.             }
  235.  
  236.         /* **** Set the size of data input field ** */
  237.         else
  238.  
  239.             {
  240.  
  241.             createElement.size = attrib[8];
  242.  
  243.             };
  244.  
  245.         };
  246.  
  247.     /* **** Create new element **************** */
  248.     parentExist.appendChild(createElement);
  249.  
  250.     };
  251.  
  252.  
  253. /* **************************************** */
  254. /* **** End: Load DOM Elements ************ */
  255. /* **************************************** */
  256.  
Oct 27 '07 #1
14 5148
gits
5,390 Recognized Expert Moderator Expert
hi ...

at first a small hint ... you may use the object 'attrib' that you have created an other way:

Expand|Select|Wrap|Line Numbers
  1. var attrib = {
  2.     id   : 'whatever',
  3.     prop1: 'whatever2'
  4. };
  5.  
  6. // to loop and alert the property-name and its value
  7. for (var idx in attrib) {
  8.     alert(idx + ' = ' + attrib[idx]);
  9. }
  10.  
  11. // refer to a property by name to alert its value
  12. alert(attrib.id);
  13.  
so you have to refer by name instead of refering by index when you want to check a property value ...

i think your script is not working ... because attrib[0] etc. is always undefined ... rewrite your code the way the example shows you ... and it should work :)

kind regards
Oct 27 '07 #2
RMWChaos
137 New Member
so you have to refer by name instead of refering by index when you want to check a property value ...

i think your script is not working ... because attrib[0] etc. is always undefined ... rewrite your code the way the example shows you ... and it should work :)
Aha! Okay, thanks again, gits. I will give this a try later on today. You'll make an expert JS coder out of me yet. =D
Oct 27 '07 #3
gits
5,390 Recognized Expert Moderator Expert
Aha! Okay, thanks again, gits. I will give this a try later on today. You'll make an expert JS coder out of me yet. =D
:) ... post back in case you have more problems with it ...
Oct 27 '07 #4
RMWChaos
137 New Member
click 'ok'...repeat

:) ... post back in case you have more problems with it ...
click 'ok'...repeat

That was a really MEAN joke, gits!!!

click 'ok'...repeat

Now my browser is locked in an infinite loop of alerts thanks to the recursion problem.

click 'ok'...repeat

Next time, tell me which code you want me to change and which code is just for example.

click 'ok'...repeat

I'll find a way to get you back!

click 'ok'...repeat

Mark my words.

click 'ok'...repeat
Oct 27 '07 #5
gits
5,390 Recognized Expert Moderator Expert
... now :) 'ok' whats the problem now? you should try to trace what attrib[0] is ... it will be undefined ... it think ... so use attrib.id or attrib.src etc. instead ...
Oct 27 '07 #6
RMWChaos
137 New Member
... now :) 'ok' whats the problem now?
Just another one of those ID-10-T errors I seem to cause all the time. Things are going better now. The recursion error is gone, thanks to your advice: attrib.id, etc. fixed it.

Now just trying to find out why "parentExist has no properties", which occurs in the very last line of code to create the element. I will figure it out, though, and let you know.

Thanks again for coming to my rescue!
Oct 28 '07 #7
RMWChaos
137 New Member
Well, I am beginning to get a clue about what is wrong here. When I run any of these alerts:

Expand|Select|Wrap|Line Numbers
  1. alert(document.getElementsByTagName('BODY')[0]); // Returns 'undefined'
  2. alert(document.getElementsByTagName('BODY').item(0)); // Returns 'null'
  3. alert(document.body); // Returns 'undefined'
  4. alert(typeof document.getElementsByTagName('BODY')[0]); // Returns 'undefined'
  5. alert(typeof document.getElementsByTagName('HEAD')[0]); // Returns [object]
  6.  
HEAD works, BODY doesn't in either IE, FF, or NN, even if I put the code directly in my html wrapped in <script>.

Wha-!? What is going on here?
Oct 28 '07 #8
RMWChaos
137 New Member
DUH!!!!

Now it all makes sense. The reason that my scripts can't see the body element is because it hasn't been instantiated yet! During testing, I was calling my scripts from within HEAD, and the page hadn't loaded BODY yet.

DOH! smacks forehead
Oct 28 '07 #9
gits
5,390 Recognized Expert Moderator Expert
you should always use the body's onload event to start scripts that are supposed to do dom-manipulations

kind regards
Oct 28 '07 #10
RMWChaos
137 New Member
you should always use the body's onload event to start scripts that are supposed to do dom-manipulations
Everything I've read says that you do not want to use <body onload="">, but for other reasons that I don't recall. Maybe we should have an open discussion on that under a new topic and see what the community has to say about it. I would sure like to hear their opinions on the matter. Additionally, I can't use onload because I have multiple scripts to load.

I have re-enabled my addEvent() script, which handles all my script loading for me; so all is mostly okay now. Still a few bugs to work out, though...as always.

Thanks again!
Oct 28 '07 #11
gits
5,390 Recognized Expert Moderator Expert
its a simple matter to load multiple scripts onload ... simply write an init function that calls everything you need to have performed ... and i did never hear of something that says that we shouldn't use the body's onload of a page. it is the best place to have a reliable point to use the dom of a page ... there are some onloads that are not that good :) the onload of images for example ... but to use the body's onload event is the common way of starting scripts for a page ...
Oct 28 '07 #12
RMWChaos
137 New Member
Okay, so it wasn't really everything I've read, but I did read something that discussed why it is a "bad" thing to use onload, probably for the reason you mentioned.

I've seen it done this way inside the body (on MSDN) too:

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT FOR=window EVENT=onload LANGUAGE="JScript">
  2.  
Oct 28 '07 #13
gits
5,390 Recognized Expert Moderator Expert
it's probably not a good idea to use the handlers that standard-way like:

Expand|Select|Wrap|Line Numbers
  1. <node onclick="func()"/>
because it is much better to use a generalized event handling ... therefore you may use attachEvent() and addEventListener() ... i've never saw the line you refer ... and i don't think that it complies to any standard ... and i think that there is no reason (and i didn't mention any :) ) not to use the body onload ... i always use it and i never had any difficulties with it ...
Oct 28 '07 #14
RMWChaos
137 New Member
I meant what you said about using onload for images, because it wouldn't load them until the page renders completely, and that can make the page appear sluggish.
Oct 29 '07 #15

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

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: Andrew Edwards | last post by:
Gentlemen, I have a Binary Search Tree ADT that should use recursion for all operation except for isEmpty() and isFull(); I have completed the insert function, and after inserting the numbers...
14
by: Torbjørn Pettersen | last post by:
I get the following error: "You left off a required attribute of the indicated element. The most common such omitted attribute is the ALT attribute of the AREA or IMG element; browsers will...
9
by: Christian Hubinger | last post by:
Hi! I've implemented some DropDown list in ASP.NET that use Ajax to fetch the data from the server. The javascript is written to call cascading to the bottom most dropdown in order to update...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
1
by: Sylaris | last post by:
hi, i have a problem which recursion fits perfectly, however; after working with the base function (which has no errors and works correctly) the recursions return a "function not defined" error in...
10
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
0
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,...
0
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,...
1
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...
0
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...
0
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.