Reputation: 1662
I am currently creating a website and have some javascript that works in all browsers except IE7 and IE8. I have done some tests on the code by inserting several 'alert' statements and deduced that the javascript breaks at one particular 'if' statement. It is not the code within the 'if' statement either because I have also tested this.
I can't see anything wrong with the actual 'if' statement myself but please let me know if there is a problem with IE7/IE8 and the code I have produced. The code can be seen below.
Thanks in advance for any help.
var Items = new Array("a","b","c","d");
var queryString = window.location.search.substring(1);
if(Items.indexOf(queryString) != "-1"){
//code goes here
}
Upvotes: 0
Views: 1184
Reputation: 22247
Here is one way of extending the Array object to support indexOf in those browsers that don't support it. Doing this has its own issues, if you ever iterate an array via for (x in a) (not suggested) and don't check hasOwnProperty this will cause you problems.
if(!Array.indexOf){
Array.prototype.indexOf = function(obj){
for(var i=0; i<this.length; i++){
if(this[i]==obj){
return i;
}
}
}
}
Upvotes: 0
Reputation: 413709
There's no "indexOf()" function on IE's Array prototype. If there were, it'd return a numeric value and not a string.
You can find an "indexOf()" polyfill at the MDN documentation page for the function.
Also, when you declare and initialize arrays, use array constant notation:
var Items = ["a", "b", "c", "d"];
Upvotes: 4