javascript 在 iframe 中包含 jQuery 时 Internet Explorer 9(不是早期版本或其他浏览器)中的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6528946/
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
Error in Internet Explorer 9 (not earlier versions or other browsers) when including jQuery in an iframe
提问by invertedSpear
Basically I have a page that launched a fancybox iframe. In that iframe I also include jQuery. But when I testing it in Internet Explorer 9 it gives me the error
基本上我有一个页面启动了一个fancybox iframe。在那个 iframe 中,我还包含了 jQuery。但是当我在 Internet Explorer 9 中测试它时,它给了我错误
Line: 68\n Error: 'Object' is undefined`
行:68\n 错误:“对象”未定义`
This error is in file jquery-1.4.1.js
and the line in question is the second line of this:
此错误在文件中jquery-1.4.1.js
,有问题的行是第二行:
// Save a reference to some core methods
toString = Object.prototype.toString,
hasOwnProperty = Object.prototype.hasOwnProperty,
push = Array.prototype.push,
slice = Array.prototype.slice,
indexOf = Array.prototype.indexOf;
which is the last lines of the var jQuery = function( selector, context ) {
.
这是var jQuery = function( selector, context ) {
.
No problem if I'm in compatibility view, no problem in any other browsers, just Internet Explorer 9 in Internet Explorer 9 mode and Internet Explorer 9 standards. If I ignore the error I see no problems using any version of jQuery.
如果我在兼容性视图中没问题,在任何其他浏览器中都没问题,只是 Internet Explorer 9 模式下的 Internet Explorer 9 和 Internet Explorer 9 标准。如果我忽略该错误,我认为使用任何版本的 jQuery 都没有问题。
What's up, and what I can do to fix?
怎么了,我能做些什么来解决?
采纳答案by invertedSpear
D'oh, thought I had the latest fancy box. Turns out I had 1.3.1, 1.3.4 fixes this issue.
天啊,我以为我有最新的花哨的盒子。原来我有 1.3.1、1.3.4 修复了这个问题。
UPD(sompylasar):In fact, that was fixed in 1.3.2 (see the changelog). I've compared the source code of 1.3.2with 1.3.1and found the following difference which solves the issue:
UPD(sompylasar):事实上,这已在 1.3.2 中修复(请参阅更改日志)。我比较了1.3.2和1.3.1的源代码,发现以下差异解决了这个问题:
content.find('iframe').attr('src', isIE6 && /^https/i.test(window.location.href || '') ? 'javascript:void(false)' : 'about:blank');
where isIE6
evaluates to:
其中isIE6
评估为:
isIE6 = $.browser.msie && $.browser.version < 7 && !window.XMLHttpRequest,
This snippet should be executed before removing the iframe
from the DOM.
这个片段应该在iframe
从 DOM 中删除之前执行。
回答by zarkobehar
The problem was fixed when I removed the SRC attribute from the iframe and I added onOpen event to jQuery's dialog:
当我从 iframe 中删除 SRC 属性并将 onOpen 事件添加到 jQuery 的对话框时,问题得到了解决:
open: function()
{
document.getElementById("mainFrame").src = "/login.aspx";
}
回答by ssawchenko
I had the EXACT same issue, but the cause of mine was different. I figured I'd share here in case others were in the same boat as me - especially given that it took me about 4 hours to track down...
我有完全相同的问题,但我的原因不同。我想我会在这里分享,以防其他人与我在同一条船上 - 特别是考虑到我花了大约 4 个小时才找到...
Our main page had an iframe which ended up having its src attribute changed twice in a very short amount of time (we were actually loading the same src twice by accident - once directly in the HTML and then again on page load via JavaScript). I'm not sure why Internet Explorer 9 chokes, but I'm guessing that the frame was partially through initializing its scripts when the src was set again, putting the JavaScript engine into an unhappy state. Either way I'm glad I found it, since we shouldn't have been loading the frame twice!
我们的主页有一个 iframe,它的 src 属性在很短的时间内改变了两次(我们实际上是意外加载了两次相同的 src - 一次直接在 HTML 中,然后再次通过 JavaScript 加载页面)。我不知道为什么 Internet Explorer 9 会卡住,但我猜测框架部分是通过在再次设置 src 时初始化其脚本,使 JavaScript 引擎进入不愉快的状态。无论哪种方式,我都很高兴我找到了它,因为我们不应该两次加载框架!
However, the real reason why Internet Explorer 9 died so bad is a mystery.
然而,Internet Explorer 9 死得如此惨烈的真正原因是一个谜。
回答by Stijn de Witt
This is a related issue: IE9 throws exceptions when loading scripts in iframe. Why?
这是一个相关的问题: IE9 在 iframe 中加载脚本时抛出异常。为什么?
There seems to be some real issue in Internet Explorer with native global objects such as 'Function' and 'Object' being undefined in some circumstances when scripts are loaded early (in the head section) of an iframe...
当脚本在 iframe 的早期(在头部部分)加载时,Internet Explorer 中似乎存在一些实际问题,在某些情况下,本地全局对象(例如“函数”和“对象”)未定义...
I placed a bounty on that other question to get some more attention to it.
我在另一个问题上悬赏以让更多人关注它。
From experience I know this issue also exists in IE8, but it was more rare there it seems. Looks like in IE9 it has gotten worse...
根据经验,我知道这个问题也存在于 IE8 中,但似乎在那里更罕见。看起来在 IE9 中它变得更糟......