Unknown runtime error in Internet explorer?

If you’re getting an “unknown runtime error” in internet explorer while using the javascript method object.innerHTML=”…some html”, here is a hint to put you on the right track: you’re probabily trying to put a block-level element inside an inline element.
For example, given the html code:

<p id="the_container">Hello</p>

<script type="text/javascript">
<!--
var obj=document.getElementById('the_container');
try
{
obj.innerHTML='<ul><li>List item 1</li><li>List item 2</li></ul>'; 
}
catch(ex)
{
alert(ex.message);
}
//-->
</script>

In internet explorer, a dialog will pop up complaining about an “unknown run-time error” (Thanks Microsoft for your helpful error messages). The solution (if you really can’t force your users to use Firefox :)) is to sobstitute the parent element with a block-level one, such as a DIV:

<div id="the_container2">Hello</div>
<script type="text/javascript">
<!--
var obj=document.getElementById('the_container2');
try
{
obj.innerHTML='<ul><li>List item 1</li><li>List item 2</li></ul>';
}
catch(ex)
{
alert(ex.message); // never displayed
}
//-->
</script>

68 Responses to “Unknown runtime error in Internet explorer?”

  1. IE tends to confuse an element’s Name property and Id property. It means that, when there is an input element as <input name=’someelement’> and a div element as <div id=’someelement’>, getElementById(‘someelement’) may return the <input> element instead of the <div> element.

    so it’d be helpful to look into the TagName of the element you are trying to set the innerHTML, as the following example:

    catch(ex) {
      alert(obj.tagName);
    }

  2. Nice that you can nest block elements inside an [a] in the HTML but can’t do it by updating via JS. IE8’s out now, can’t MS kill 6? Normally I’d hate so say “Microsoft, please force an update on me via Windows Update” but this is one exception to that.

  3. When in doubt, switch from Ps to DIVs. No idea why that has any bearing on anything because what I was doing was using Prototype’s “wrap” function to wrap an A in another DIV then insert a second DIV inside the first div ahead of the A (an absolutely-positioned overlay over the original A which contained an image, using the “wrapper” div which had its position set to relative to allow it to line up correctly).

    It worked fine in some instances but not in others (got the “unknown error” or NO ERROR AT ALL (zero feedback, it just refused to do what I was telling it to — nice!) depending on how I wrote it).

    I finally noticed that the ONLY difference was that the ones that worked had a DIV as the immediate ancestor of the A but the ones that didn’t had a P as the ancestor. Switching to DIVs fixed it (just plain old P and DIV tags in the HTML, no CSS applied, not written in dynamically…).

    Why the parent element had any effect at all I can’t imagine because all of the DOM changes were further down the tree, and the wrap wasn’t the problem, it was inserting that second div which was then inside a div so it shouldn’t have had this problem, but it is what it is.

  4. I just had this problem in IE8, when I was trying to put Ajax form in a DIV. I just had forgotten that form tags where already opened around the div.
    They could at least rename this to “Web standarts error” or something, it would be more helpful.

  5. I googled ‘getelementbyid Ie6 runtime error’ and this was the first search result. I had exactly the same problem you are having in your blog post and I thank you for pointing out this solution.

    -Justin

  6. big help, thanks for the post. Dumb to require email addresses for comments, though.

  7. Thanks, my error was I had a <a tag surrounding div tag (it was a same page #anchor tag); closed the tag before starting the div and all was well

  8. Hi,

    I am facing same problem.I am using div but still error is coming.
    Please help me out.

    My code is

    function updatepage(str,arg){
    	
    	//alert(str);str=html element
    
    	var obj=document.getElementById('sub3');
    	try
    	{
    		obj.innerHTML=str;
    	}
    	catch(ex)
    	{
    	alert(ex.message); // never displayed
    	}
    }
    
  9. No need for fanboyish Microsoft Bashing, please.

    And BTW, MSIE8 still does this (same error message), so it’s not an MSIE6 issue.

  10. Another data point:

    In IE7, I was trying to replace a table via AJAX:

    getElementById(“table”.innerHTML = http.responseText;

    and IE7 gave this error.

    When I enclosed the table in a :

    <div id="tableContainer"

    then *both* of these worked:

    getElementById(“table”.innerHTML = http.responseText;

    getElementById(“tableContainer”.innerHTML = http.responseText;

    HTH

    — ben

  11. Let’s try that again. Another data point:

    In IE7, I was trying to replace a table via AJAX:

    getElementById(”table”.innerHTML = http.responseText;
    …
      <table id="table">
        ...
      </table>
    
    …
    

    and IE7 gave this stupid “unknown runtime error” message.

    When I enclosed the table in a <div>:

    <div id="tableContainer">
      <table id="table">
      </table>
    </div>
    

    then *both* of these worked great:

    getElementById(”table”).innerHTML = http.responseText;
    
    getElementById(”tableContainer”).innerHTML = http.responseText;
    

    That is, I could replace both the table and the table container by their IDs.

    HTH

    – ben

  12. Good advice… I googled the topic, and before i looked, i just tried to use the .html() and .append() jquery functions, which worked beautifully… so there is solution numero dos

  13. 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.

  14. This gave me a starter for ten – thanks for posting it.

  15. What is a runtime library and what is a runtime error? How can I correct this problem?

  16. hi,

    i got the error “htmlfile: Unknown runtime error” in IE 8, when i upgrade my IE version to IE9 it doesn’t run my web application(shows IE has stopped working message).

    In other browsers it working fine but from few hrs it is giving this error in IE at the line-

    e.innerHTML=”var __chd__ = {‘aid’:11079,’chaid’:’www_objectify_ca’};(function() { var c = document.createElement(‘script’); c.type = ‘text/javascript’; c.async = true;c.src = ( ‘https:’ == document.location.protocol ? ‘https://z’: ‘http://p’) + ‘.chango.com/static/c.js’; var s = document.getElementsByTagName(‘script’)[0];s.parentNode.insertBefore(c, s);})();”

    i am using deveexpress control and javascript for popup window.

  17. Fantastic.. i have been trying yo fix this for 3 hrs..
    Ur solution solved my problem

  18. I had this same issue except my problem was that I was using hidden inputs with innerHTML. It worked great in Firefox but IE caught the error and stopped the script.

    My solution (thanks to this article and comments) was to use a span with a display:none style.