WebDevelopment, ColdFusion, Railo, JS, Database and Tech-related by the Co-Founder and CEO of tunesBag.com

Wednesday, 2 December 2009

Add missing Array.indexOf to Internet Explorer

When dealing with JSON it comes in handy to get the position of a certain string in an array, image an array with column names returned by a SerializeJSON'ed CFQUERY.

Internet Explorer does not support this function, but just add the following snippet and you can use Array.indexOf( 'TITLE' ) in any browser without built-in support as well (We add a new function to the Array prototype!)

// make sure indexOf is supported
if(!Array.indexOf){
Array.prototype.indexOf = function(obj, start){
for(var i=(start||0); i<this.length; i++){
if(this[i]==obj){
return i;
}
}
}
}

2 comments:

Brett said...

Nice... Just don't forget to return -1 if it fails...

Whitney said...

Thxxx this was very useful, yet another bizarre IE inconsistency.