Monday, May 20, 2024
154
rated 0 times [  159] [ 5]  / answers: 1 / hits: 179338  / 15 Years ago, mon, november 16, 2009, 12:00:00

If you have worked with JavaScript at any length you are aware that Internet Explorer does not implement the ECMAScript function for Array.prototype.indexOf() [including Internet Explorer 8]. It is not a huge problem, because you can extend the functionality on your page with the following code.



Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) { return i; }
}
return -1;
}


When should I implement this?



Should I wrap it on all my pages with the following check, which checks if the prototype function exists and if not, go ahead and extend the Array prototype?



if (!Array.prototype.indexOf) {

// Implement function here

}


Or do browser check and if it is Internet Explorer then just implement it?



//Pseudo-code

if (browser == IE Style Browser) {

// Implement function here

}

More From » internet-explorer

 Answers
222

Do it like this...



if (!Array.prototype.indexOf) {

}


As recommended compatibility by MDC.



In general, browser detection code is a big no-no.


[#98300] Thursday, November 12, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
yaquelina questions
;