Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript .includes() not working in IE

Tags:

javascript

I am working on a function which looks through a list of table values on the page and if the column title contains any part of the output, it will convert the value accordingly.

function createLink(field, val) {

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

var i, key, keys = Object.keys(output);
for ( i = 0; i < keys.length; ++i ) {
        key = keys[i];
  if ((field.toLowerCase()).includes(key)) {
     return '<a href="'+output[key]+'" target="_blank">'+val+'</a>';
  }
}

return val;
}

The issue I am running into is that IE is throwing an error on the .includes() line stating that "Object doesn't support property or method 'includes'".

I had a bit of trouble getting it to work as is but didn't realize includes() must be something that not all browsers support.

Is there something else I can use in place of this that will allow for cross browser support ?

like image 813
SBB Avatar asked Jun 01 '17 19:06

SBB


2 Answers

includes() is part of the ES6 specification, so it's not supported in IE. What you can use instead is indexOf(element) !== -1

like image 192
Freeman Lambda Avatar answered Sep 30 '22 12:09

Freeman Lambda


replace:

if(field.toLowerCase().includes(key)) {

to:

if(field.toLowerCase().indexOf(key) > -1) {
like image 45
num8er Avatar answered Sep 30 '22 10:09

num8er