Javascript – Why does forEach() not work in an iframe in IE11?


This question was closed as off-topic, so I've marked the missing info.

Specific problem or error plus shortest code neccesary:

The code below doesn't work in IE11, if it's in an iframe of certain websites.
("Certain" is not specific, but I don't have a public demo. I can't make one, until I find the cause in my private code. Still, the question is specific enough to be answered by an expert, that's why I've asked SO instead of a long debugging process without any idea.)

['a', 'b'].forEach(function(elem){console.log(elem);});

The error says that the array doesn't support the forEach method.

Desired behavior:

The forEach() method executes a provided function once per array
element. – MDN

Best Solution

"IE11 uses Quirks Mode emulation if the top-level page is not in Edge Mode." - MSDN

In this mode, arrays don't support the forEach method.

Use a simple for loop instead, or write this right after the title tag of the parent:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />