Javascript – how to overcome IE bug when dynamically attaching div to body from script

dhtmldominternet-explorerjavascript

UPDATE: the HTML was not well formed. This was causing the script to appear in inner div. Please ignore the question.

IE ver<8 has a known bug (Internet explorer cannot display the page. Operation aborted)
If a script tries to append something to upper level block:

The bug is described here:

Update: I rephrased the question and simplified the example:

The bug occurs in the following code:

[end of html file]
<script type="text/javascript" >
if (window.document.body){
    var c_div = window.document.createElement('div');
    window.document.body.appendChild(c_div);    
}
</script>

</body>

Question: This seems to me exactly similar to example1 Method1 In Microsoft workaround (here is the link again). How come I still have the bug? What am I missing here?

Best Solution

Run your code in a domready/onload event handler.

Related Question