Reputation: 3903
could someone maybe tell me why I'm getting the error Unable to get property 'substr' of undefined or null reference
? I only get this error on IE browsers (including Edge) - all other browsers work fine:
share.getMedia = function (arrClassList) {
var mediaType;
if (arrClassList.length > 1) {
for (var i = 0; arrClassList.length >= i; i++) {
if (typeof arrClassList[i] != "undefined") {
var currentString = arrClassList[i].substr(2);
if (currentString.indexOf(arrTemplates)) {
mediaType = currentString;
}
}
}
} else {
if (typeof arrClassList[0] != "undefined") {
var currentString = arrClassList[0].substr(0, 2);
if (currentString.indexOf(arrTemplates)) {
mediaType = currentString;
}
}
}
return mediaType;
};
UPDATE: when I do console log the arrClassList
I indeed get an object:
{
0: "social",
1: "facebook"
}
Does somebody know what the issue is here? It only happens in IE + Edge browser...
Upvotes: 1
Views: 4090
Reputation: 3390
This is not a solution to your question. But wanted to write here instead of comments for better readability.
arrClassList
is object, and .length
property does not work on objects. So the if
condition will return false
. However you can excess the object value by using arrClassList[0]
or arrClassList['abc']
in case the key is of type string.typeof arrClassList[0] != "undefined"
will return true but you should use identity ===
operator instead of equality ==
operator.0
key has a value that is not equal to null/undefined
you should get your substr
.arrTemplates
. If it is a string, that should work fine. If it is an array of strings, and there is only one string in that array, it should work. If it is an array of multiple strings, it won't work.For why it is not working on IE and Edge, while working on others. My theory would be that different browsers use different javascript engines, and these engines in some cases try to make your code work even if there are small issues. IE and Edge both use a JScript engine named Chakra
(although very similar, JScript and Javascript have their differences). And my guess is that there are issues in your code that Chakra is unable to resolve.
Upvotes: 3