views:

961

answers:

3

The setup:

I have a suckerdiv menu with links that call a function. Said function looks like:

function F(string)
{
    var s = '';
    var c = '';
    var t = '';
    if(string == 'cat')
    {
        s = "cat";
        c = "animal";
        t = "fluffy";
    }
   // ...
    document.getElementById("title").innerHTML = t;
    document.getElementByID("category").innerHTML = c;
    document.getElementByID("description").innerHTML = s;
}

works fine. however, in the actual implementation, the description variable has a little bit of HTML in it, and it seems to be throwing IE off. firefox treats it just fine, but if I have, say,

c = 'Stuff<br><ul><li>listed stuff</li><li>more listed stuff</li><li>some more listed stuff</li></ul><br>bla bla<br>bla';

then IE throws an Unknown Runtime Error and dies when I click that particular link.

the three IDs above are divs that are nested in a table like so:

<table border=1>
<tr><td><div id="title"></div></td></tr>
<tr><td><div id="category"></div></td></tr>
<tr><td><div id="description"></div></td></tr>
</table>

I know that tables by themselves are read-only in IE, but I also know that TDs aren't. the title and category both work fine, so I figured it had something to do with the html in that string. Is there some part of it that I should be escaping? I tried filling it with html-escape codes like < etc, but then it just didn't evaluate the HTML and I was left with a bunch of ugly text.

Any ideas?

+1  A: 

All I can guess is that you may want to try self-closing your BR tags: <br />

This is one of those situations that often vanish when you use a library like Prototype or jQuery for your DOM manipulation. Browser quirks are usually taken into account internally.

Diodeus
I actually had them like that originally, but my validator yelled at me for it, haha. :(I love jQuery, and I'd have used it here, but I didn't feel the need - it's a very small script in the middle of an automatically generated page. If I can't find a workaround I'll definitely use it, though.
Yeah, those IE error message are soooo helpful sometimes.
Diodeus
A: 

I created the following test case:

<html><head></head><body>
<script type="text/javascript">
function F(str) {
  var s = '';
  var c = '';
  var t = '';
  if (str == 'cat') {
    s = "cat";
    // c = "animal";
    c = 'Stuff<br><ul><li>listed stuff</li><li>more listed stuff</li><li>some more listed stuff</li></ul><br>bla bla<br>bla';
    t = "fluffy";
  }
  // ...
  document.getElementById("title").innerHTML = t;
  document.getElementById("category").innerHTML = c;
  document.getElementById("description").innerHTML = s;
}
</script>
<table border="1">
<tr><td><div id="title"></div></td></tr>
<tr><td><div id="category"></div></td></tr>
<tr><td><div id="description"></div></td></tr>
</table>
<form><input type="button" onclick="F('cat');"></form>
</body></html>

It appears to work correctly in Internet Explorer 7. Are you sure there isn't something you are doing in // ... that is causing your error? Have you confirmed this happens with every copy of Internet Explorer 7 (your copy may have an add-on, corrupt or outdated component or incorrect registry entry causing the error you are seeing). Have you narrowed the problem down to assigning the string to innerHTML using a simple and concise code sample such as that shown above?

Grant Wagner
The content of // ... is just duplicates of what's there - more "if str == 'dog'" type of content. I tested it on another computer in the office, and it didn't work.The reason I think it's that line specifically is because running a debugger pointed the error toward "document .. HTML = c;".
I routinely assign HTML 740 characters long, complete with comments and inline CSS to innerHTML in IE7 and it always works. I'm sure the HTML you supplied isn't what you are using, so start with a very simple example (c = '<span>test</span>';) and build up from there until it stops working.
Grant Wagner
It actually is the same - well, kind of. The tags are the same; I just took the content out. So instead of "Stuff" it says "Welcome to this fabulous div!" or something like that. I'll try building up one section at a time, though.
Update: I started simple - c = 'This is a <br />test.';and it broke. So I guess it's not that line, which puts it in the category of "this is too simple to break." How frustrating!
+1  A: 

Doh!

Sorry for wasting your time, guys - turns out it was totally unrelated. The line was breaking because of the div's id; since the HTML in the middle of the page, nested in a containing div generated by a content management system, apparently there was another div somewhere else in the page. Changing "div id=description" to "div id=dynDescription" solved it.

Thanks for the answers, though! Definitely appreciated.