www.webdeveloper.com
Results 1 to 6 of 6

Thread: while

  1. #1
    Join Date
    Oct 2006
    Posts
    939

    while

    I don't use while often because I don't understand it and consequently, I crash my machine. Please re-write this using while
    HTML Code:
    if(document.getElementById("tray").hasChildNodes()){
    	document.getElementById("tray").removeChild(document.getElementById("tray").getElementsByTagName("img"))
    }

  2. #2
    Join Date
    Jul 2009
    Posts
    72
    I would have tried with a for loop here, try this:

    Code:
    if(document.getElementById("tray").hasChildNodes()){
    	for(var i=0; i<document.getElementsById('tray').getElementsByTagName('img').length; i++) {
    		document.getElementById("tray").removeChild(document.getElementById("tray").getElementsByTagName("img")[i])
    	}
    }
    didnt test it, so dunno if there are any bugs or typos

  3. #3
    Join Date
    Oct 2006
    Posts
    939
    ah well, thought I'd try to learn something new but,,
    document.getElementsById('tray').innerHTML=""
    works well enough. I don't know, firefox crashes more often than IE on some of my inventions.

  4. #4
    Join Date
    Feb 2006
    Posts
    2,927
    A while will continue to loop as long as the condition is true-
    just make sure the condition cannot be true forever...

    Code:
    var pa= document.getElementById('tray');
    var nodes= pa.getElementsByTagName('img');
    while(nodes.length){
        pa.removeChild(nodes[0]);
    }

  5. #5
    Join Date
    Oct 2006
    Posts
    939
    So, Mrhoo, would that be a better way to do it than innerHTML="" ?

    I put in a button to alert(document.images.length)
    and I get expected results. Is there memory leak or, why would innerHTML="" be an inferior way to do it?

    BTW, my project keeps moving along,, I'm just wishing to employ a whole lot of DOM stuff that I've not bothered with before like,

    appendChild(node)
    cloneNode(deepBoolean)
    removeChild(childreference)
    replaceChild(newChild, oldChild)

    I wanted to set an onload function and tried this after appendChild(node)
    node.setAttribute(attributename, value, [iecaseflag])

    But, couldn't get it to work and it was easier to to have document.write('<img src="'+Q[i]+'" onload="nextFunc()"> ')

    So I dunno. Guess I'm stuck with methods that will not be supported someday?
    Last edited by justinbarneskin; 11-01-2009 at 04:15 PM. Reason: spell

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by justinbarneskin View Post
    I wanted to set an onload function and tried this after appendChild(node)
    node.setAttribute(attributename, value, [iecaseflag])

    But, couldn't get it to work and it was easier to to have document.write('<img src="'+Q[i]+'" onload="nextFunc()"> ')

    So I dunno. Guess I'm stuck with methods that will not be supported someday?
    On the contrary, those methods (DOM methods) are the modern one. Can we see you code? Should work, unless you were made a mistake.

    innerHTML could be also an alternative. But it depends on the aim of your code. innerHTML is not a standard DOM method and it does not insert always all the elements and their attributes into the DOM tree. If you need a simple display of new elements (and no further DOM manipulation) you may use innerHTML. Otherwise you are forced to use DOM methods.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles