substr() in IE and Firefox

I’m sure this is documented in more detail elsewhere, but since this has caught me out a few times, I’ll document this here.

The javascript substr() function works differently in IE and Firefox.

var myString = "hello world";

In Firefox this returns “rld”, but throws an error in IE. To do the same thing in both IE and Firefox:

var myString = "hello world";
myString.substring(myString.length(-3), myString.length);

3 Responses to “substr() in IE and Firefox”

  1. Nathan Says:

    Cheers mate, that’s really helped me! I was getting so frustrated as this is forming part of a function that performs an AJAX submit on a lightbox form… How the hell does this kind of thing happen anyway? It is really unacceptable that two browsers that run the same scripted language would return different results from a core function! Aaaahhhhh!

  2. mahalay Says:

    sorry mate, that didn’t work. Here’s what worked for me:

    var str = ‘Hello World’;
    str.substring(str.length – 3, str.length);

  3. Alex Says:

    myString.length(-3) doesn’t work for me, did you mean myString.length() -3?

Leave a Reply