Javascript – IE9 throws exceptions when loading scripts in iframe. Why

asp.net-mvc, iframe, internet-explorer-9, javascript, model-view-controller

Precondition:

I have an aspx-page with iframe inside. This iframe points to the url handled by MVC on the same site (it's hybrid site, both standard ASP.NET and ASP.NET MVC). The resulting page rendered by MVC contains a lot of scripts references.

Problem:

IE9 throws an exception on every single script it load in iframe. These exceptions are similar to this one:

Error: 'Function' is undefined

That is, it says that the most basic things every window has is somehow absent. Once you clicked through all of these popups, the page just works as designed!
If I load a URL from <iframe /> src attribute in the browser directly, everything works as expected.
If I open the page in another browser (I tried Opera, Firefox), everything works as expected — no errors.

So, what IE9 wants?

Best Solution

There is this msdn page about this bug (or feature).

You get these kinds of errors when you move the iframe element around in DOM. In such cases, IE 9 garbage collects the iframe (causing your undefined bug) and reloads it at another position.

In general, you should create the element, set its src attribute only once and then put it somewhere in the DOM tree once. It has nothing to do with the code which runs in the iframe itself.