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";
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);
February 12th, 2010 at 12:09 pm
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!
June 15th, 2010 at 5:43 am
sorry mate, that didn’t work. Here’s what worked for me:
var str = ‘Hello World’;
str.substring(str.length – 3, str.length);
November 9th, 2010 at 12:15 pm
myString.length(-3) doesn’t work for me, did you mean myString.length() -3?