Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE9 throws exceptions when loading scripts in iframe. Why?

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?

like image 688
Eugene Strizhok Avatar asked Dec 05 '11 17:12

Eugene Strizhok


4 Answers

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.

like image 193
copy Avatar answered Oct 13 '22 05:10

copy


I have encountered this same situation in the wild. Basic symptoms:

  • You load script code in an iframe
  • The script code runs early (from the head section or top of body)
  • IE complains about some missing native object

I found that it can often be prevented by delaying the execution of the script code until onload or DOMContentLoaded... Not much help I know but this is one of the most difficult IE scripting bugs I have ever encountered. I upped the score of your question, hope it will be found by others as well and we can get a more detailed answer.

Also see this question: Error in Internet Explorer 9 (not earlier versions or other browsers) when including jQuery in an iframe

like image 45
Stijn de Witt Avatar answered Oct 13 '22 06:10

Stijn de Witt


Placing the following script block at the very top of the iFrame html <head> seems to resolve the issue in my case. Basically, it forces the iframe to reload, which as some have pointed out, solves the issue. It seems relatively safe, because, without things like 'Object' and 'Date', javascript is essentially useless.

<script type="text/javascript">
    if(typeof(Object)==="undefined"){
        window.location.reload();
    }
</script>
like image 2
Scott Paradis Avatar answered Oct 13 '22 04:10

Scott Paradis


Try loading the javascript at the end after complete web page is loaded. I feel the script is executing even before the iframe is completely loaded.

for some suggestion of scripting in IE9 view the given link below http://blogs.msdn.com/b/ie/archive/2010/06/25/enhanced-scripting-in-ie9-ecmascript-5-support-and-more.aspx

like image 1
Jayprakash S T Avatar answered Oct 13 '22 05:10

Jayprakash S T