yet another note for myself

Top 6 reasons why IE7 is complaining about your javascript

Your Ad Here

 

1. Source: Script tag is the first element of the page:  The html page returned by the server contains ”script” tag as the first element of the webpage. This problem occurs mostly in pages returned as a result of ajax call.

Solution: Introduce a fake div tag as the first element of the webpage. You can set the height of the div tag to zero so as to avoid any visual changes in your page. eg . < style=”height: 0px”>&nbsq;< /div > <script language=”javascript”>…..</script>

2. Source: Comma after the last element of an array: If you leave a comma after the last element of an array, it will raise an error in IE7 but not in firefox. For eg options = {‘item1′:’val1′, ‘item2′:’val2′,} will raise an error in IE7

Solution: It is obvious. Remove the damm comma.

3. Source:  Firebug console command: If you are using firebug “console” or any other command for debugging your javascript in Firefox and left these commands in your javascript, then IE7 will raise an error warning.

Solution: Again its obvious. Remove all the commands that are related to firebug javascript debugger.

4. Source: Trying to access non-existing identifier (id): if the javascript tries to access a non-existing element, it will break your javascript code in IE7.

Solution: Its difficult to identify this problem in IE7 as IE7 only reports the problem but not the element name that it is trying to find. However, it will give you a line number and character number as a part of the warning which can help in narrowing you search for the missing element.

5. Source: Defining variable without “var”:I was surprised to find this one. But within my document block for jquery, I defined a variable without “var” modifier and this raised an issue in IE7. Here is how my document block was originally coded

$(document).ready(function(){

         library = “hello”;

        message = “world”;

       if(“form#” + library).submit(….

        ….

     );

 });

Solution: Use “var” modifier to define all you javascript variables. In the above case, after I added “var” modifier in-front of library and message variable, everything seemed to be working fine.

6: Source: <script /> tag : IE expects script tag to be XML compatible format. That is, it expects both starting (<script>) and ending <script/>) tag. Using shorthand for script tag, i.e, <script /> might create problems in IE. Thanks to Simon for reporting this problem. 

Solution: Always use starting (<script>) and ending (</script>) tag. 

Enjoy

Related posts:

  1. Cannot modify header information – headers already sent
This entry was posted in Javascript and tagged . Bookmark the permalink.

38 Responses to Top 6 reasons why IE7 is complaining about your javascript

  1. Jamien says:

    Last one was my issue.
    Thanks for this.

  2. ragrawal says:

    I am glad that my post helped you.

  3. someone says:

    Since firebug lite (a pure JS bare-bones version of firebug) came out, step #3 is no longer required. Of course you should do this for production code regardless. The others are good tips though, thanks.

  4. Jacob Ensor says:

    OMGGG THANK YOU SO SO SO SO SO SO MUCH!

    #5 was my problem. Again, thanks so much, I was getting verrrry aggravated. I spent hours coding something then found out it didn’t work in IE. Then I spent hours searching for a solution to no avail. This really helped me out, thanks so much.

    Jake

  5. Pingback: forge numérique – Javascript et Internet Explorer: 5 erreurs courantes

  6. Dave Miller says:

    #5 was my problem too. title = "X"; didn’t work, but var title = "X"; and mytitle = "X"; both did. “title” seemed to be an object or DOM element that I wasn’t allowed to assign anything to. :-S

  7. I use the following to keep IE from complaining about Firebug console.log statements:

    if (!window.console){console={log:function(){}}};

    I usually put this as the first line in my scripts.

  8. Erich Schmid says:

    for #5 it is interesting to know that IE by default creates a global JS variable for every HTML element with an ID. E.g.

    ....

    gives you a global variable called “myvar” in your JavaScript code. You can – if you write pages that are only ever used on IE – use this to e.g. change its color:

    myvar.style.backgroundColor = "red";

    but more annoyingly you’ll get an error on IE every time you accidentally use a JS variable in your code with the same name, unless you declare it with var.

    myvar = 3; // error

    var myvar = 3; // OK

    I think that’s the actual cause of these IE problems.

  9. Erich Schmid says:

    sorry, HTML code doesn’t show up in code block, the “…..” just shows a div with id=”myvar”

  10. Brent says:

    Again, reason #5 (missing the “var” in front of variable declaration) worked for me. Thanks for the tips!

  11. Erik says:

    Ahh! Damn you comma at the end of the array!

    Thanks for this post!

  12. Manuel says:

    I found a really weird bug that I think should be added to #5. I was trying to define a constant in the first line of my code, but it didn’t work. All I had was the following:

    const AJAX_TIMEOUT = 3000;

    I changed it to:

    var AJAX_TIMEOUT = 3000;

    and it started working perfectly. That is really retarded.

  13. Ezeamama Andrew says:

    Thanks this was very helpful

  14. simon says:

    After a few days of panic I found another one:

    if you close the tag this way firefox and safari would go fine

    for ie you need to use

  15. simon says:

    After a few days of panic I found another one:
    if you close the tag with just the slash at the end of the line
    firefox and safari would go fine
    for ie you need to use the closure with a new “/script” tag

  16. josh says:

    thanks a bunch…was tearing my hair out. extra commas and console.log were the cause.

  17. Tatzoof says:

    Hello, thanks for this fix !

    You should add another point : I had bugs because of reserved word, like ‘class’ which I use in JSON objects. IE don’t want to use it (throw ‘id, string or number expected”)

  18. arioss says:

    thanks a lot….

    my problem is that damn comma in the end of array (#2) … <_<

  19. Dmitry Dulepov says:

    Any variable that is created inside a block without “var” keyword becomes a global variables. All global variables in turn are properties of the “window” object. So “title = ‘Hello’” is equivalent to “window.title = ‘Hello’”, which can be disallowed by the browser for security reasons.

    This is a reason for having “var” always before variables. Firsts, you guarantie that you do not override a window property. next you do not create new global variables.

  20. "marc says:

    sorry to be precise, but hee, it’s a debug page right: “ending ) tag.” should be “ending ) tag.

  21. Melinda says:

    Thank you so much! I found both the extra commas in the arrays *and* the vars not declared with var. This was very helpful!

  22. Matt S says:

    Thank you! IE is a piece of crap. You are awesome.

  23. Jason says:

    Thank you so much for this! You rock. I thought I was in for hours of trying to get IE up. Well it was a stupid comma on the end of this line:

    gridview : true,

    Seriously IE? crap.

    took the comma off and wah-lah. Ok thank god now I can move on to the next hours of trouble shooting.

    Thanks again!

  24. Pingback: rustbeltlabs.net » Blog Archive » Fixing Javascript Bugs in IE7

  25. rob-ot says:

    tnx a million !!

  26. mcgyver says:

    Thanks ZZillion times
    You saved my day…I was trying for hours to get it to work in IE7
    I found the extra commas in the arrays ….
    IT WORKED
    You are awesome
    Kind Regards
    Jean from France

  27. Dakota says:

    Thanks for the good summary. Turns out it was a stray comma. Best, D

  28. dana says:

    Thank you! I had the #5 problem and your list just saved me a ton of time!

  29. THANK YOU!
    You just saved a LOT of debugging time.

  30. Kris Leech says:

    When developing locally it seems you can not load javascript from the web eg. Google CDN.

  31. Ryan Cross says:

    Thanks Heaps. #2 tripped me up since I’m used to doing it in PHP for coding standards.

  32. Jejuna says:

    #2 was the culprit in my case. It’s something I would have never thought of in a million years. Thanks so much for posting this article and for keeping it around.

  33. Hemal says:

    #5. is really help me, I have variable idx which are not declared using var key word and give me a problem in both IE 6 with version:6.0.2900.2180.xpsp_sp2_gdr.050301-1519 and IE 7 but surprisingly it does not give problem with IE 8 and other IE 6 browser. Still the I do not know the exact the root cause of the issue.

  34. Remo says:

    Hi,

    Thanks for all listing of solution for IE7 bugs.

    the first one solved my problem.

    Thanks ones again

  35. Hulio says:

    Foiled by a comma yet again!

    Thanks for the list!

  36. srw says:

    Another reason to add to your list is related to sort:

    See http://www.zachleat.com/web/2010/02/24/array-sort/

  37. This has been a wonderful and insightful read, I will be recommending it to my friends thanks!

  38. Martin says:

    Thanks a lot, #2 sorted me out in a flash :)