Top 6 reasons why IE7 is complaining about your javascript

This blog post is now moved over 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

44 thoughts on “Top 6 reasons why IE7 is complaining about your javascript

  1. 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.

  2. 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

  3. #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

  4. 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.

  5. 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.

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

  7. 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.

  8. 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

  9. 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

  10. 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”)

  11. 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.

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

  13. 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!

  14. 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

  15. #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.

  16. #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.

  17. Hi,

    Thanks for all listing of solution for IE7 bugs.

    the first one solved my problem.

    Thanks ones again

  18. Number 2 was mine!

    FF, Opera, Safari, Chrome will all cruise happily over the top of a misplaced comma, considering it a minor inconvenience. PHP even goes “look! There’s a comma I wasn’t expecting… right THERE… LOOK!”.

    IE6 and IE7, on the other hand, suddenly throw out their cot toys, cry and start holding their breath! It’s up to you to try and figure out what the problem is.

    Having had two children, the similarities between IE and a baby having a tantrum are startling!

    Thanks for this list, man… a great resoure! :)

  19. Does IE7 supports following code for declaring variabels?
    var a, b;

    or do you have to write:

    var a;
    var b;

  20. In case it is of all help to anyone, in IE7, the jQuery .removeAttr(‘href’) simply makes the href an empty string, whereas in other browsers it makes the href undefined.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.