JavaScript Support and Array.indexOf in IE
JavaScript 1.6 was released in November 2005, and even though Internet Explorer 7 was released almost a year later in October 2006, it still uses the older JavaScript 1.5. I only discovered this fact recently when I started using Array.indexOf() in some lightbox code. It was all working fine in Firefox, but as soon as I started testing it in IE, it stopped!
indexOf() returns the index of the first occurrence of an item in an array. Previously we would have needed to write something like:
searchString = 'red';
colours = ['green', 'red', 'blue', 'red', 'orange'];
var found = -1;
for (var i = 0; i < colours.length; i++) {
if (colours[i] == searchString) {
found = i;
break;
}
}However, now with Array.indexOf(), it's much simpler:
searchString = 'red';
colours = ['green', 'red', 'blue', 'red', 'orange'];
var found = colours.indexOf(searchString);Except, IE doesn't support it! It's also quite likely that Opera and Safari don't support it also, but the following methods should work for all of these browsers:
if (!Array.indexOf) {
Array.prototype.indexOf = function (obj, start) {
for (var i = (start || 0); i < this.length; i++) {
if (this[i] == obj) {
return i;
}
}
}
}While looking into this issue, I learned a lot more about JavaScript versions and its history. For example, what do the JavaScript versions mean? And why don't all browsers implement them? JavaScript was originally developed by Netscape back in 1995. At the time it was called LiveScript, but was later renamed to JavaScript in a co-marketing deal between Netscape and Sun. Due to the widespread success of JavaScript, Microsoft developed their own compatible version and called it JScript. Netscape then submitted JavaScript to Ecma International for standardisation resulting in the standardised version named ECMAScript. EMCAScript is the recognised standard and the latest version of it is implemented by all modern browsers, including IE 6+, Firefox 1+, Opera and Safari.
The latest version of ECMAScript is 3 and it corresponds to JavaScript 1.5. JavaScript 1.6-1.9 are just code names for the interim versions of ECMAScript (implemented by Gecko browsers) that are leading up to JavaScript 2 (EMCAScript 4), and so other browsers are not obligated to implement them. However, Opera and Safari both implement some of the additional features (but not all) introduced in JavaScript 1.6 - 1.9.
The next version of ECMAScript, version 4, is still a work in progress, but is expected to be published in October later this year. More details on the ECMAScript 4 proposal can be found here.


When prototyping, the new method should extend all the functionality of the existing method, in order to use it completely transparent. In this case, the "start" parameter is missing. Obviously, no such a big deal but being perfectionists…
true religion
Also, it should return -1 in the case the item is not found.