Saturday, March 17, 2007

Top 4 reasons why your javascript is not working in IE7

JQuery is an awesome javascript library that has made writing ajax application a piece of cake. However, building a perfect working application is still a problem, mainly, due to different standards of different browsers. Espeically, IE7 is notorious. There are no good free javascript debuggers for IE such as firebug for firefox and often IE does not stick with web standards. So here is a list of things/mistake that can screw up ajax your application in IE7.

Top 4 reasons why your javascript is not working in IE7

1. Script tag is on the first line of the page:
If you ajax page has javascript in it and the script tag is the first line (or first character of your page), then it is highly likely that IE7 will fail. To overcome this issue, I generally introduce a div tag (I refer it as fake div, because I set its height to 0px)as the first thing on my ajax page.
Solution to problem 1: < style="height: 0px">&nbsq;< /div >

2. Comma after the last element of an array:
Often jquery plugins can take arguements in the form of an array. However, make sure that you do not introduce a comma after last element. Firefox can handle it, but not IE. I believe the reason is that comma indicate that there is another element that will be following it. Thus IE expects that more elements are still coming and wait for them. So
This is wrongThis is right
{ name: 'Jack', message: 'hello', }{ name: 'Jack', message: 'hello'}

3. using firebug and console command
Many times developers use Firebug, a Firefox addon. I often use "console" command to debug my scripts in firefox. However, if by chance you left any console command within javascript script, it will break your javascript in IE7. Make sure to comment out or remove all "console" command before you test your javascript in IE7.

4. Trying to access non-exisitng key
if the javascript tries to access non-existing element, it will break your web site in IE7. Firefox will work without raising or reporting any error or warning.