0

The below JavaScript If/Else statement works correctly in identifying if the browser is IE11, with the result being that in IE the console prints "Internet Explorer" and in all other browsers prints "Not Internet Explorer":

if (window.document.documentMode) {
  console.log("Internet Explorer");
}
else {
  console.log("Not Internet Explorer");
}

However if I place something like a Fetch in the Else statement (which is not compatible with IE and should also not be read by IE) I will get a Syntax Error in the IE console:

if (window.document.documentMode) {
  console.log("Internet Explorer");
}
else {
  console.log("Not Internet Explorer");
  fetch('https://randomuser.me/api/?results=10')
    .then(response => response.json())
    .then(data => console.log(data));
}

SCRIPT1002: Syntax error pointing to the first response of the Fetch:

.then(response => response.json())

Could someone advise what I'm doing wrong?

neptr
  • 15
  • 3
  • Does `fetch` work OR are you using a polyfill for that? – adiga Jun 08 '20 at 10:29
  • 1
    You don't use Arrow Functions for IE. Please check from this link the bottom. Look page bottom. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – huseyinturkmenoglu Jun 08 '20 at 10:30
  • 1
    @adiga Fetch don't work for IE. (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) But if you need to use it, you can use babeljs etc.. – huseyinturkmenoglu Jun 08 '20 at 10:32
  • @hüseyint Thanks for the info regarding Arrow Functions. It appears that the whole statement is being read by IE and throws the Syntax Error even in the Else statement. Did you want to post your answer and I'll mark it as correct? – neptr Jun 08 '20 at 10:41
  • @adiga The fetch works. The issue is that when I use an If/Else to identify IE11 that IE still executes the code in the Else statement. However given that IE doesn't use arrow functions it's clear that the whole If/Else statement is read by IE (giving the syntax error) even if it's not executed. Learnt something :) – neptr Jun 08 '20 at 10:56
  • @Rubio your question closed. Please look already has answers from above. – huseyinturkmenoglu Jun 08 '20 at 12:29
  • @hüseyint Yeah I noticed that afterwards. Thanks again for your quick response anyway. – neptr Jun 08 '20 at 12:48

0 Answers0