1

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...

ST80
  • 3,565
  • 16
  • 64
  • 124
  • It depends on what arrClassList contains. You may want to make clear what data is in there or how it gets there. – Norman Sep 02 '15 at 11:50
  • @Norman the `arrClassList`contains an array of strings, so maybe thats the issue? if so, how to avoid it? Like mentioned, the issue only appears in IE + Edge – ST80 Sep 02 '15 at 12:05
  • What output do you get when you alert the element before IE displays the error? – Norman Sep 02 '15 at 12:23
  • @Norman see my update on the original question :-) – ST80 Sep 02 '15 at 12:38
  • possible duplicate of [substr() with negative value not working in IE](http://stackoverflow.com/questions/6918943/substr-with-negative-value-not-working-in-ie) – Marcos Pérez Gude Sep 02 '15 at 12:39
  • @MarcosPérezGude how can this be a duplicate? – ST80 Sep 02 '15 at 14:37

1 Answers1

3

This is not a solution to your question. But wanted to write here instead of comments for better readability.

  1. Arrays are not equal to objects. What you shared as the value of 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.
  2. typeof arrClassList[0] != "undefined" will return true but you should use identity === operator instead of equality == operator.
  3. If 0 key has a value that is not equal to null/undefined you should get your substr.
  4. The issue that I see is with 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.

Jehanzeb.Malik
  • 3,332
  • 4
  • 25
  • 41