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.
By Theo Gray on June 1, 2009 | Permalink | Comment
Reader Comments
Skip to form
March 27, 2010
,Kelly McIvor says:Thanks for the post! This was precisely what what happening for me. I was trying to replace innerHTML with a <p> element. Using two <br> did the trick.
Kelly
October 6, 2010
,Mark Brodsky says:I had this same problem with the "Unknown Runtime Error" in IE 8 - code worked fine in Firefox and Safari...
But, I was not illegally placing a block element into an inline or other element. I was simply updating the innerHTML of a SPAN within a surrounding DIV. So no problem, right?
Pulled my hair out trying to figure out what was happening until I thought maybe I was using a reserved keyword.... Sure enough - my SPAN ID was 'type'. I changed it to 'typer' and it all worked fine.
The funny thing about this is that it WAS working fine. Must have been a Windows update that caused it to suddenly fail.
November 5, 2010
,Mike says:Thanks for this post :)
February 16, 2012
,Dries Knottnerus says:I also had this "unknown runtime error" in the div3.innerHTML statement in this code:
var div3 = document.all.item("div3");
div3.innerHTML = "";
but it worked when I changed the all.item to getElementById:
var div3 = document.getElementById("div3");
div3.innerHTML = "";
March 30, 2012
,Jean-Marc says:Great post, tx.
September 25, 2012
,Potheek says:Thanks a lot, that was helpful :)
December 5, 2012
,Emily says:I was trying to update innerHTML of table. Had to change it to a div to work in IE8. Rest of the browsers I am testing with worked fine with table.
July 17, 2013
,Deepak cheela says:Problem of displaying div using innerHTML using AJAX And Javascript
In IE8
Please ...............
resolvel my Problem
October 22, 2013
,Ashish says:I have hosted my website but its not fully loaded in IE
November 15, 2013
,ashok kumar singh says:Thanks for this usable post
December 11, 2013
,Pharmacy Joe says:THANK YOU!!!!!!!!!!!!!!!!
July 16, 2014
,Philip says:THANK YOU SO MUCH!
I saw this error and I could not for the life of me figure out what was wrong with my code. I was trying to put elements into a <section></section>...section...
Worked on IE11 FF and Chrome, but not IE8.