IE8’s Substr() Bug

I thought I would share this gotcha in the hope it saves the rest of the world some time. Here’s the deal……

In IE8 and below when using substr() to parse a string you cannot use -1 as the startIndex parameter: it will return NaN and error out. As an example, this will not work:-

var index = id.substr(-1, 1);

Instead you would need to do the following:-

var index = id.substr(id.length-1, 1);

 

Very small Yay!

Advertisements

1 comment so far

  1. Benito Aramando (@aramando) on

    Nice clean solution. Much more concise than Mozilla’s suggestion of overriding the substr function! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr#Description


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

%d bloggers like this: