A JavaScript behavior difference between Firefox and IE

I just found out the following difference of behavior between Firefox and IE. Here is the input JavaScript:

if(a = b)
    // do something

Firefox will second guess what the programmer was really trying to do and treat the code in the following way:

if(a == b)
   // do something

IE will not attempt to translate the code and comparing the two variables. Instead, it will re-assign the variable a as the code literally stated. This is just one of many examples that shows Firefox is a lot more forgiving and attempts to render or execute incorrectly written code. IE shows strict behavior and will not work unless the code is syntactically correct.

Advertisements

12 thoughts on “A JavaScript behavior difference between Firefox and IE

  1. Though it looks that firefox is the good boy, who tries to understand what I was trying to say and does so; I hope this behavior stays at a very low level assumptions only.

    A language should be strictly followed, else people will start writing junk knowing that it will work. This will lead to bugs which are very difficult to trace and resolve, as there wont be a fixed behavior intended for the system.

  2. aakashd,

    What you feared is already a reality. Fortunately, these are all known issues and there are either known solutions or workarounds for many of these issues to allow web developers to create web applications that work across different browsers. 😉

  3. This is an odd case, usually it is IE that is very forgiving, and actually breaking specs.

    In this case here, the code is bad, and Firefox is fixing it. I don’t have issue with that. What I don’t like is when the code is valid, and IE decides that it knows what I really want, and does something else. document.getElementById() is the most classic case of the simplest method being ruined by a bad browser implemetation.

    see here for details:
    http://webbugtrack.blogspot.com/search/label/152

  4. I found that the character counter (in the form of regular expression or javascript ‘.length’ method) when used in IE counts differently than Firefox, specifically when it has new line or blank line in the content.

    For instance, the counter on http://www.javascriptkit.com/script/script2/charcount.shtml
    generates different counts for the same content (include a couple of blank lines in the content) depending on the browser used. Firefox counts new line character as 1 character, while IE counts as 2 characters.

    I’m trying to validate a text area in my web form and running into this issue. I would like the form to validate the same content the same way in both IE and firefox. Is there a way to resolve this?

  5. Hi sunrays,

    What if you do a pre-processing step that removes blank lines and other white spaces before running the char counting code? Does the count still differ between the two browsers? If that doesn’t help, you can always write browser specific code by using a browser detection code. Hope this helps.

  6. I ran across the character counter issue in a system I am currently assigned to. It seems that text from textboxes in Firefox or Safari just returns a single asc(10) character for LFCR pairs whereas IE returns an asc(13) and an asc(10). You need to add code to the function to ensure that the character count reflects having an asc(13) for every asc(10) in the text string. I use a little function that takes a string a returns the difference between the two. I add this difference to the character count to get one that works.

    /******************************************************************************
    getLFCRDiff

    Purpose:
    Counts the number of line feeds and carriage returns in a text object and returns the difference.

    Argument List (I=Input, O=Output, I/O=Input/Output):
    I textstr string to count LFs and CRs.

    Returns:
    int difference of LF and CR counts.
    ******************************************************************************/
    function getLFCRDiff(textstr)
    {
    lfcount = 0;
    crcount = 0;
    countdiff = 0;
    for (i=1;i < textstr.length;i++){
    a = textstr.charCodeAt(i);
    if (a == 10){
    lfcount++;
    }
    if (a == 13){
    crcount++;
    }
    }
    if (lfcount != crcount){
    countdiff = lfcount – crcount;
    }
    return countdiff;
    }

  7. Hey,

    I ran into the same firefox/explorer problem with counting chars, and i would like to thank you for the script. I simplified it a bit though, seeing as the premise is that there will always be a asc(10), but not always an asc(13).

    **************************************************

    function getLFCRDiff(textstr)
    {
    countdiff = 0;
    for (i=1;i < textstr.length;i++){
    a = textstr.charCodeAt(i);
    if (a == 10){
    countdiff++;
    }
    if (a == 13){
    countdiff–;
    }
    }
    return countdiff;
    }

  8. Well, I too have run into same char count issue. Looking at the solutions above.. Dont you guys implicitly assume that the ‘textstr.length’ will give you the total length to count. If FF computes this length counting CRLF as one.. Am not sure but I guess we.. either “Iterate over only a prefix of the string” or “do not see two chars for CRLF”.. No?

  9. @Dave:

    “In this case here, the code is bad, and Firefox is fixing it.”

    Not necessarily. Assignment inside the conditional clause is a valid technique for streamlining code. If that’s the intention, then FF breaks the code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s