Archive for October, 2007

substr() in IE and Firefox

Monday, October 22nd, 2007

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";
myString.substr(-3);

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);