Blog
Internet Explorer and the "Unknown runtime error"
As a developer I've come across my fair share of annoying Microsoft error messages, so I wasn't too suprised when I randomly hit the following Javascript error in IE8:
Unknown runtime error
Even better, if I switched to IE7 compatibility mode I got the old:
[object error]
Having tracked down the line that was causing the problem, it appears that the Javascript hook into the DOM is rather more strict (or maybe buggy!) when setting innerHTML than when using document.write.
document.write
The following line of code works and displays the expected output:
document.write('<p id="extraNote"><div class="somethingSpecial">Hello</div></p>');
innerHTML
And the following works as expected in all browsers except IE:
// Done when the page is initially displayed
document.write('<p id="extraNote"></p>');
// Done in a seperate function that works out what should really go in extraNote
var note = document.getElementById("extraNote");
note.innerHTML='<div class="somethingSpecial">Hello</div>';
The fix
Having worked out why IE was being annoying, the fix was very simple...
replace <p id="extraNote"> with <div id="extraNote">
... but it's obvious IE8 still has random IEisms, and proves that when it comes to supporting IE, that even now you have to fully test even the most minor of code changes (extraNote used to be a table cell rather than a paragraph i.e. <td id="extraNote">).
Update: Just checked the IE Programming Bugs Wiki and see that this is apparently "by design" - "Can't put invalid HTML in the document: You cannot assign a string to innerHTML or outerHTML that contains invalid HTML. For example, trying to replace the content of the P element with another P will fail. A P element can only contain text and inline elements. However, replacing the entire P element with another P would work just fine."
This seems like a very wierd limitation, especially as it's possible to edit the DOM directly and create a <div> within a <p> tag in IE, but there we go. Case closed I guess.


Reader Comments
Skip to form
There are currently no comments about this article.