/    Sign up×
Community /Pin to ProfileBookmark

indexOf in < IE9

IE8 reports indexOf is not a supported method. I’m using this to get the value of an array item.

However, MS say IE6+ should be supported; [url]http://msdn.microsoft.com/en-us/library/53xtt423(v=vs.94).aspx[/url]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@bionoidAug 18.2012 — All String objects/literals have a indexOf method, if you however passed in a number or boolean etc, then you will have to convert it to String() first:

[CODE]alert( [COLOR="red"]'Test'[/COLOR].indexOf('e') ); //1
alert( [COLOR="Red"]String([/COLOR][COLOR="Blue"]123[/COLOR][COLOR="red"])[/COLOR].indexOf('3') ); //2[/CODE]
Copy linkTweet thisAlerts:
@ldoodleauthorAug 18.2012 — I am passing a string already;

j = b.indexOf(photoUrl);

with photoUrl being a parameter, such as /images/subfolder1/subfolder2/image.jpg

Maybe the / in the string are ****ing it up?
Copy linkTweet thisAlerts:
@bionoidAug 18.2012 — But what is the value of "b", as that is where the problem would b ?
Copy linkTweet thisAlerts:
@Logic_AliAug 18.2012 — IE8 reports indexOf is not a supported method. I'm using this to get the value of an array item.

However, MS say IE6+ should be supported; http://msdn.microsoft.com/en-us/library/53xtt423(v=vs.94).aspx[/quote]


That article refers to String().indexOf not Array().indexOf which is of much more recent introduction.

Here's the crutchware that you need:if( Array().indexOf === undefined )
{
Array.prototype.indexOf = function( item )
{
for( var i = this.length - 1; i &gt; -1 &amp;&amp; this[ i ] !== item; i-- )
;

<i> </i>return i;
}
}
Copy linkTweet thisAlerts:
@ldoodleauthorAug 19.2012 — That article refers to String().indexOf not Array().indexOf which is of much more recent introduction.[/QUOTE]

Thankyou very much. I have seen other variants of your code but they didn't seem to work.

?

EDIT: I noticed yours uses triple = rather than double. Why? Also some of the other articles on the MS website use triple (http://msdn.microsoft.com/en-us/library/jj155295(v=vs.94))
Copy linkTweet thisAlerts:
@Logic_AliAug 22.2012 — I noticed yours uses triple = rather than double. Why? Also some of the other articles on the MS website use triple (http://msdn.microsoft.com/en-us/library/jj155295(v=vs.94))[/quote]

Strict type checking is always good practice.

Actually I now realise that code isn't right, and having looked at the specs, the function allows an optional starting index to be used, so I re-wrote it:
<i>
</i>
Array.prototype.indexOf = Array.prototype.indexOf || function( item, idx )
{
var retVal = 0,
startIndex = ( typeof idx === 'number' ? Math.abs( Math.floor( idx ) ) : 0 );

for( var i = startIndex, len = this.length; i &lt; len &amp;&amp; this[ i ] !== item; i++ )
;

return i == len ? -1 : i;
}

×

Success!

Help @ldoodle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...