Clearing the contents of a DOM Element

Sometimes we get lazy, we decide not to care about a couple of K of our user's RAM and we do silly things like replacing the content of a DOM Element with innerHTML. It's not a huge deal, in fact I'm a fan of using the AHAH method of inject pre-formatted XHTML into my document when memory consumption is small or not an issue. AHAH is easy. You can take advantage of all the fancy parsing resources you have with with serverside development, expose no actual data to the client. It's quick and dirty, and that's often times ok. Thing is, when you are writing an application that may ping a webservice and update content on a regular basis (whether it's an AIR application or just something that runs in a browser) it's not cheap. Since you can't have quick, cheap and dirty, you end up paying for the repeted injection of chunks and use of innerHTML through memory loss, and browser performance suffers.

CODE:
  1. var Someobject = {
  2.     clearElement : function(el){
  3.         if (!el) return false;
  4.         var el = (typeof el === 'string') ? document.getElementById(el) : el;
  5.         var i = 0;
  6.         while (el.hasChildNodes())
  7.         {
  8.           el.removeChild(el.firstChild);
  9.         }
  10.         delete el;
  11.         return true;
  12.     }
  13. };

Make sure you know what you're removing

If you're not using firebug or firebug lite, or a webkit nightly. You might want to be able to generate console-like output to give you a clue as to what elements you're actually removing from the DOM. If that's the case, you can just add something like this trace function to SomeObject.

CODE:
  1. var Someobject = {
  2.     trace : function(str){
  3.         if (window.console) {
  4.             return console.log(str);
  5.         } else {
  6.             if (!document.getElementById("clearElementOutput")){
  7.                 var output = document.createElement("div");
  8.                 output.id = "clearElementOutput";
  9.                 document["body"].appendChild(output);
  10.             }
  11.             // dirty
  12.             document.getElementById("clearElementOutput").innerHTML += str + "<br />";
  13.             return true;
  14.         }
  15.        
  16.     },
  17.     clearElement : function(el){
  18.         if (!el) return false;
  19.         var el = (typeof el === 'string') ? document.getElementById(el) : el;
  20.         var i = 0;
  21.         while (el.hasChildNodes())
  22.         {
  23.           //comment this out to suppress output
  24.           this.trace(i++ + "deleting " + el.firstChild.nodeName);
  25.           el.removeChild(el.firstChild);
  26.         }
  27.         delete el;
  28.         return true;
  29.     }
  30. };

A demo file can be found here.

Viewing 1 Comment

Trackbacks

close Reblog this comment
blog comments powered by Disqus

Nater Kane naterkane personal http://www.naterkane.com LinkedIn Profile Web Technologist personal nater@naterkane.com 1978-09-12 voice 845.234.6698 | fax 707.922.0593
964 Flushing Ave. Brooklyn, NY 11206