Monday
Nov 20 2006
Porting JavaScript to Internet Explorer
While porting a small AJAX application I wrote to Internet Explorer, I encountered a few problems:
-
IE doesn't like
<script>tags without a matching closing tag. This is especially a problem if you use thesrcattribute and try to use an XHTML-like closing tag like this:<script src="..."/>
In this case IE doesn't draw anything at all, since it keeps looking for the end of the script and doesn't find it. This is especially a problem with templating toolskits like Kid. If Kid encounters a construct like
<foo></foo>it will correctly shrink this to<foo/>in the XML output. The solution is rather easy: Insert a newline character between openening and closing tag in the template. Since Kid has now way to determine whether this whitespace is significant it will keep it in the output. -
It seems that IE doesn't like trailing commas in JavaScript object literals:
var o = { x: 'Foo', y: 1, };
I like to insert trailing commas in cases like this. It's easy to forget to add one if extending the property list. Firefox parses the trailing comma, while IE doesn't. (IE follows the ECMAScript standard here, which doesn't allow the trailing comma. Indeed in array literals a trailing comma has a special meaning, so it's probably a good idea to disallow the trailing comma in object literals.)
-
My AJAX application queries a web service for a list of items and displays that list in a table. The table is generated in HTML code like this:
<table id="mytable"> <tr>...</tr> </table>I then used JavaScript code like the following to populate that table:
var table = document.getElementById("mytable"); var trTag = createRow(); table.appendChild(trTag);
This worked fine in Firefox, but no matter what I did, IE refused to render the table rows. After much hair tearing I remembered that Firefox's DOM Inspector showed a
TBODYelement just beneath theTABLEelement andTRelements are attached to the former. The DOM Inspector basically showed the following structure:-
TABLE id=mytable-
TBODY-
TR(my header row from the HTML)
-
-
TR(a dynamically generated row) - ...
-
So I changed my code to append the newly generated rows to the
TBODYinstead:var table = document.getElementById("mytable"). getElementsByTagName("tbody")[0]; var trTag = createRow(); table.appendChild(trTag);
This works in Firefox and IE.
-
Comments
Wow, you saved me
by Peter
Saturday, 2006-12-02 17:51
After hours of banging my head against the wall and pulling my hair out, your comments saved me! I never would have figured out the table thing on my own. Thank you!
Great info
by Andrew
Tuesday, 2006-12-05 23:26
Thanks for the heads up on this. IE can make you go bald very quickly.
WOW!
by RenanWB
Saturday, 2007-01-06 00:43
Great initiative! You helped me a lot!
sweet
by Jesse
Wednesday, 2007-03-07 20:17
one of the more helpful pages on the differences between IE and FF when it comes to javascript. Thanks a lot.
Thanks
by Sunny
Wednesday, 2007-05-16 15:47
Thanks this blog is really helpful me... :)
by Sunny Chevli
Similar fix for col and colgroup
by Peter Thatcher
Tuesday, 2007-06-05 15:51
I have found that just as you need TBODY wrapped around TR for IE to handle dynamic tables correctly, you also need COLGROUP wrapped around COL for IE to respect width settings for columns correctly, especially when using tableLayout=fixed.
This web page saved me hours of grief and I hope this little comment of mine may spare someone else.
This is so true :)
by Dan Tirdea
Saturday, 2007-10-27 09:04
Lucky me to find this page. This is EXACTLY what i needed. Thank you so much!
Comments for this article have been disabled.