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

浪子不回头ぞ 提交于 2019-12-11 14:58:50

问题


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?


回答1:


Run your code in a domready/onload event handler.




回答2:


It might be a slightly insane approach but could you pull out the innerHTML of the entire <body> append your string to it and then set the <body> innerHTML to the new value?

May affect some previously attached JS events unless it's inline onclick stuff.




回答3:


The HTML in the page was not well formed. Please ignore the question.



来源:https://stackoverflow.com/questions/551511/how-to-overcome-ie-bug-when-dynamically-attaching-div-to-body-from-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!