This article is written for IE developers, which means that the JavaScripts exmaples are basically only tested on my own IE6 & IE7.
It's always better to manipulate the DOM contents of an Element especially when you try to remove or add Nodes to the Element.
If we would like to modify the child nodes of an Element before is fully ready to be modified, then something interesting may happen. For example:
The result will look like this:
So here's the problem, the text 3.Third Line should comes after the text 2 . Second Line since we use appendChild() to add new Nodes to the Element.
appendChild Method
The appendChild method appends elements to the end of the childNodes collection.
The reason for this problem is very simple, just because that the DOM tree has not loaded the entire HTMLof this Element and it turns out that the text 1.First Line would be considered as the last child of the Element instead of the text 2.Second Line .
Besides, one more annoying thing is that you may crash IE if you have JavaScripts like this:
This problematic codes will crash IE from displaying the original HTML contents and prompt a error message "Operation Abort" which is critical enough for anyone to access the web page.
In order to prevent that, it's always better to defer the JavaScript action until the Element is fully parsed.
So how do we knows that the DOM of an Element is fully parsed? One of the interesting way is read the outerHTML of the Element.
For Internet Explorer, you shall actually see its outerHTML like this from the alert(dEl.outerHTML) dialog when DIV#foo has not read its close tag yet.
The interesting thing is that the close Tag of the Element is missing at this moment.
Unlike all the other major A-Grade browsers, if you try to read the outerHTML of an Element while it's being fully parsed in runtime, then you might not get the close tag at the end of its outerHTML in Internet Explorer.
To simplify the code, let's ignore the HTML of its childNodes.
Then we will get
Ah ha, we have a DIV Tag without close tag here!
To see the difference when the outerHTML is fully loaded, let's try this in IE.
Then we will get (for IE)
Therefore, it seems like that we may check whether an Element has close tag in its outerHTML periodically to see if this Element is fully parsed and ready for DOM manipulation.
For example, here we have two functions, onElementAvailable() and onElementContentReady(). The test case would like to append a new textNode 3.Third Line after 2.Second Line . We first check if Element#foo is available then check if it's ready periodically. you may see the example codes below and a live test here.
So it looks like we have learned somthing interesting here, but how solid or useful will it be if we can normalize this methodology since all the tricks always have their own limitation and cost?
I did another test to see if this rule applies to all the HTML Elements and I realized that there're some tags always carry the close tag within their outerHTML.
The Last (But Certainly Not The Least) important part ff this article is that now I feel much safer to implement CSS expression() on IE in order to fix some wacky CSS issues or attach new behaviors. You may see how the example here and see if how do I apply dynamic style to those Elements.
Have fun.
Hedger Wang