4

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

Tamás Bolvári
  • 2,976
  • 6
  • 34
  • 57
  • 5
    You're going to have to post the code involved for anybody to be able to help. – Pointy Nov 21 '14 at 16:12
  • I can only guess, since you haven't provided a reproduction. My guess is the frame issued this command. `delete [].constructor.prototype.forEach`. That removes the `forEach` method from all arrays. – recursive Nov 21 '14 at 16:14
  • 3
    Do you check that the [document mode](http://msdn.microsoft.com/en-us/library/ie/dn255001%28v=vs.85%29.aspx) is at least 9? IE8 and earlier versions didn't support `forEach` Read [this](http://stackoverflow.com/questions/13519696/mvc-app-causing-ie9-to-use-older-standards/13524518#13524518). – dusky Nov 21 '14 at 16:24
  • @Pointy, code is added. – Tamás Bolvári Nov 21 '14 at 16:30
  • @recursive I control the iframe: the mentioned command is not issued. I'm not responsible for the iframe's parent, but if the command is issued by that: it doesn't affect the iframe. – Tamás Bolvári Nov 21 '14 at 16:32
  • @dusky The document mode of the form (when not in iframe) is Edge. The document mode of the container website is 7 (caused by a X-UA-compatible meta tag). Does it affect the iframe? It seems to me. I'll read your resources to see if I can avoid this. Thank you for your help! – Tamás Bolvári Nov 21 '14 at 16:38
  • 2
    Yes, the iframe is affected by the parent's settings (even if I set edge mode explicitly in a meta tag). "IE11 uses Quirks Mode emulation if the top-level page is not in Edge Mode." - http://msdn.microsoft.com/en-us/library/ff955402(v=vs.85).aspx – Tamás Bolvári Nov 21 '14 at 17:26
  • @Pointy, I've reworded the question to fit the rules. – Tamás Bolvári Dec 01 '16 at 06:06

1 Answers1

10

"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" />
Tamás Bolvári
  • 2,976
  • 6
  • 34
  • 57
  • 1
    Remember, avoid using IE=edge unless you are sure what you are doing. http://stackoverflow.com/questions/14611264/x-ua-compatible-content-ie-9-ie-8-ie-7-ie-edge http://www.gwtproject.org/doc/latest/DevGuideIE9.html – Chexpir Aug 31 '16 at 08:52
  • I disagree, I say avoid _not_ using IE=edge :) – Yes Barry Mar 22 '19 at 19:28