javascript IE9 在 iframe 中加载脚本时抛出异常。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8389261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE9 throws exceptions when loading scripts in iframe. Why?
提问by Eugene Strizhok
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.
我有一个里面有 iframe 的 aspx 页面。此 iframe 指向同一站点(它是混合站点,标准 ASP.NET 和 ASP.NET MVC)上由 MVC 处理的 url。MVC 呈现的结果页面包含大量脚本引用。
Problem:
问题:
IE9 throws an exception on every single script it load in iframe. These exceptions are similar to this one:
IE9 会在 iframe 中加载的每个脚本上引发异常。这些异常与此类似:
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.
也就是说,它说每个窗口所具有的最基本的东西都以某种方式不存在。单击所有这些弹出窗口后,该页面将按设计运行!
如果我<iframe />
直接从浏览器中的 src 属性加载 URL ,一切都会按预期工作。
如果我在另一个浏览器中打开页面(我尝试过 Opera、Firefox),一切都按预期工作——没有错误。
So, what IE9 wants?
那么,IE9 想要什么?
回答by copy
There is this msdn pageabout this bug (or feature).
有这个MSDN网页有关这个错误(或功能)。
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.
当您在 DOM 中移动 iframe 元素时,您会遇到这些类型的错误。在这种情况下,IE 9 垃圾收集 iframe(导致您未定义的错误)并在另一个位置重新加载它。
In general, you should create the element, set its src
attribute only onceand then put it somewhere in the DOM tree once. It has nothing to do with the code which runs in the iframe itself.
通常,您应该创建元素,src
只设置一次它的属性,然后将它放在 DOM 树中的某个位置一次。它与在 iframe 本身中运行的代码无关。
回答by Stijn de Witt
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
- 您在 iframe 中加载脚本代码
- 脚本代码运行较早(从头部或正文顶部)
- IE 抱怨缺少一些本机对象
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.
我发现通常可以通过将脚本代码的执行延迟到 onload 或 DOMContentLoaded 来防止它...我知道的帮助不大,但这是我遇到过的最困难的 IE 脚本错误之一。我提高了你的问题的分数,希望其他人也能找到它,我们可以得到更详细的答案。
Also see this question: Error in Internet Explorer 9 (not earlier versions or other browsers) when including jQuery in an iframe
另请参阅此问题: 在 iframe 中包含 jQuery 时 Internet Explorer 9(不是早期版本或其他浏览器)中的错误
回答by Scott Paradis
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.
在 iFrame html <head> 的最顶部放置以下脚本块似乎可以解决我的问题。基本上,它强制 iframe 重新加载,正如一些人指出的那样,解决了这个问题。它看起来相对安全,因为如果没有像“Object”和“Date”这样的东西,javascript 基本上是无用的。
<script type="text/javascript">
if(typeof(Object)==="undefined"){
window.location.reload();
}
</script>
回答by Scott Paradis
Further investigation revealed that the solution is to add the offending iframe to it's dom location BEFORE setting the 'src' attribute.
进一步调查显示,解决方案是在设置 'src' 属性之前将有问题的 iframe 添加到它的 dom 位置。
Once the 'src' has been set, changing location of the iframe within the DOM stack forces IE9 to garbage collect it.
一旦设置了“src”,在 DOM 堆栈中更改 iframe 的位置会强制 IE9 对其进行垃圾收集。
Once 'src' has been set, iframe can be resized and changed via css positioning, but cannot change the relative location in the DOM stack.
一旦设置了 'src',iframe 可以通过 css 定位调整大小和更改,但不能更改 DOM 堆栈中的相对位置。
Often times, plugins like dialogs and lightboxes will stuff an iframe with src already set into the dom, then append / prepend or whatever, triggering the GC to take place.
很多时候,像对话框和灯箱这样的插件会将 src 已经设置到 dom 的 iframe 填充到 dom 中,然后附加 / prepend 或其他什么,触发 GC 发生。
回答by Jayprakash S T
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.
加载完整的网页后,尝试在最后加载 javascript。我觉得脚本甚至在 iframe 完全加载之前就在执行。
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
有关在 IE9 中编写脚本的一些建议,请查看下面的给定链接 http://blogs.msdn.com/b/ie/archive/2010/06/25/enhanced-scripting-in-ie9-ecmascript-5-support-and-更多.aspx
回答by Mr Mols
function waitForjQuery(){
if(typeof jQuery!='undefined'){
//Do yor stuff!
}
else{
setTimeout(function(){
waitForjQuery();
},500);
}
}
waitForjQuery();