0

I have some code that I have been using in an application for some time now where all of our internal users are using firefox for running the app. There are a few people wanting to run IE and there is a block of code that I believe is hitting an error and I don't know exactly why..

// Given a field name, check to see if its in our output. If so, return the formatted link
function createLink(field, val) {

    var output = {
        'ntid': 'https://web.internal/profile/' + val,
        'email': 'mailTo:' + val
    };

    for (var key of Object.keys(output)){
        if (field.toLowerCase().includes(key)){
            return `<a href="${output[key]}" target="_blank">${val}</a>`;
        }
    }

    return val;
}

In IE 11, I get a console error SCRIPT1004: Expected ';' which refers to the line for (var key of Object.keys(output)){.

Is this not supported in IE11 or is there some type of syntax that FF handles correctly that IE doesn't?

SBB
  • 8,560
  • 30
  • 108
  • 223
  • 2
    `for of ` doesnt work in IE you have to use normal `for` loop with btw is more efficient `for(var i =0 ;` <- this is expected ; – Maciej Kozieja Feb 21 '17 at 16:57

2 Answers2

0

Instead of the "for...of", try a "for...in" which is supported by every browser for a long time now. Syntax is exactly the same.

(There is a difference between them but I guess in your case it's not relevant... More about this here: What is the difference between ( for... in ) and ( for... of ) in javascript? )

Community
  • 1
  • 1
dkellner
  • 8,726
  • 2
  • 49
  • 47
  • I tried this simple change and got `Object doesn't support property or method 'includes'` – SBB Feb 21 '17 at 17:01
  • for... of is smiliar to for... each of AS3/ES4, except that for... each iterates values of object-kind value, and for... of uses a symbol inside object property, in order to iterate values of collections (but all values from the properties, they've to be values). –  Feb 21 '17 at 17:02
  • @handoncloud - Any chance you could share what the compatible code would be for this example? Sounds like I have more than one issue in this case after swapping `of` for `in`. – SBB Feb 21 '17 at 17:03
  • @SBB You could try `var i, key, keys = Object.keys(output); for ( i = 0; i < keys.length; ++i ) key = keys[i]...; `, but this won't be exactly the same as for... of, because it uses a symbol reserved as property in the loop object. –  Feb 21 '17 at 17:06
  • 1
    The "includes" thing is an independent problem, it's because IE is not ES6 compliant. Try indexOf()... – dkellner Feb 21 '17 at 17:10
  • 1
    ^ Yeah, for... in would also work, but you need to take the property value from the key you receive too. –  Feb 21 '17 at 17:12
0

Instead of the "for...of", try a "for...in" which is supported by every browser for a long time now. Syntax is exactly the same.

Not entirely:

https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Statements/for...of

Leon Rom
  • 537
  • 4
  • 6