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
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
Thanks for the heads up on this. IE can make you go bald very quickly.
WOW!
by RenanWB
Great initiative! You helped me a lot!
sweet
by Jesse
one of the more helpful pages on the differences between IE and FF when it comes to javascript. Thanks a lot.
Thanks
by Sunny
Thanks this blog is really helpful me... :)
by Sunny Chevli
Similar fix for col and colgroup
by Peter Thatcher
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
Lucky me to find this page. This is EXACTLY what i needed. Thank you so much!
hello
by anyngeaxy
winsome answers i like it
Independent Wp Themes
by WP Themes
Good brief and this fill someone in on helped me alot in my college assignement. Say thank you you for your information.
I am new here
by Gucci Shoes
I am new here and looking to meet some nice people.
<a href=http://www.togetshoes.com>Gucci Shoes</a>
very very very nice blog
by loans
www.rittau.org is very interesting. I enjoyed your blog a lot. Thank you.
Coach bags
by Coach outlet
http://www.guccihandbagsoutlet.com/ Gucci outlet
http://www.coachoutletmalls.com/ Coach factory outlet
http://www.guccihandbagsoutlet.com/ Gucci outlet online
http://www.coachoutletmalls.com/ Coach outlet store
http://www.guccihandbagsoutlet.com/ Gucci handbags
http://www.coachoutletmalls.com/ Coach outlet
This article is written by zocy003 on 2010-7-21 http://www.coachoutletmalls.com
yyRNpC
by yyRNpC
OuuvwEo