IE9 中 iframe 中的 Javascript 代码不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5514973/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:33:34  来源:igfitidea点击:

Javascript code in iframes in IE9 not working

javascriptasp.netinternet-exploreriframeinternet-explorer-9

提问by Diego

I've a very complete site in ASP.NET wich uses iframes. I'm working to change an old control we'd been using to show dialogs to use jQuery UI dialogs. I'm also making sure everything works well in IE9.

我在 ASP.NET 中有一个非常完整的站点,它使用 iframe。我正在努力将我们一直用来显示对话框的旧控件更改为使用 jQuery UI 对话框。我还确保在 IE9 中一切正常。

The fact is: the script I've in the pages shown in iframes is not working in IE9. Why? Because Object, Array and String are undefined. There may be some others issues, I've seen only this ones.

事实是:我在 iframes 中显示的页面中的脚本在 IE9 中不起作用。为什么?因为 Object、Array 和 String 是未定义的。可能还有其他一些问题,我只看到了这些。

There is no chance (because a lot of reasons) to stop using iframes on some dialogs. And I'd rather not to use the meta tag to force IE8 Compability. Does anyone know any way to fix this uggly bug in IE9?

没有机会(因为很多原因)停止在某些对话框上使用 iframe。而且我宁愿不使用元标记来强制 IE8 兼容。有谁知道如何修复 IE9 中这个丑陋的错误?

Thanks, Diego

谢谢,迭戈

Edit

编辑

Here there's some info that may be helfull:

这里有一些可能有用的信息:

jQuery code for the iframe in a plugin I've made to config jQuery UI dialog:

我为配置 jQuery UI 对话框而制作的插件中 iframe 的 jQuery 代码:

options.content = $("<iframe>")
    .attr("src", options.intSrcIframe)
    .attr("frameborder", 0)
    .attr("scrolling", options.intIframeScrolling)
    .css("background-color", options.intBgColorIframe)
    .attr("height", "100%")
    .attr("width", "100%");

_this.html(options.content);

回答by Diego

Note: herethere is some documentation from IE9 that may help to understand. Thanks to @Ben Amada for sharing it.

注意:这里有一些来自 IE9 的文档可能有助于理解。感谢@Ben Amada 分享它。



After almost a week of going crazy day after day I found it out.

经过将近一周的日复一日疯狂之后,我发现了这一点。

The problem with IE9 is no specifically with the javascript code in the iframes. Not even with the javascript in iframes added by javascript or any library (I have lots of js libraries and plugins that could be screwing IE9).

IE9 的问题不在于 iframe 中的 javascript 代码。甚至没有使用 javascript 添加的 iframe 中的 javascript 或任何库(我有很多 js 库和插件可能会破坏 IE9)。

The problem is when you move the iframe or one ancestor through the DOM. IE9 will excecute the code in the page loaded in the iframe as many times as moves you make. Each time (but the last one) will have Object, String, Array and others undefined (and other bugs too).

问题是当您通过 DOM 移动 iframe 或一个祖先时。IE9 将执行 iframe 中加载的页面中的代码与您所做的移动一样多。每次(但最后一次)都会有对象、字符串、数组和其他未定义(以及其他错误)。

Example:

例子:

var iframe = $("<iframe>").attr("src", "www.example.com").attr("id", "myIframe");
iframe.appendTo("body");
$("#myIframe").appendTo("form:eq(0)");

The javascript code in "www.example.com" will be executed once with the error described above and then once with no errors.

“www.example.com”中的 javascript 代码将执行一次并出现上述错误,然后再执行一次没有错误。

With the following code the code will be excecuted just once and correctly:

使用以下代码,代码将仅正确执行一次:

var iframe = $("<iframe>").attr("src", "www.example.com").attr("id", "myIframe");
iframe.appendTo("form:eq(0)");

I hope this helps someone to avoid this pain in the ass,

我希望这有助于某人避免这种痛苦,

Diego

迭戈

回答by SliverNinja - MSFT

There is a similar way of achieving this using an existing iframe if you aren't creating a new element.

如果您不创建新元素,则可以使用现有 iframe 来实现这一点。

$(function () {
    var iframeSrc = $("#iframeid").attr("src"); // capture target URI
    $("#iframeid").attr("src", "about:blank"); // delay loading until we reposition in the DOM
    $("#iframeid").appendTo("#newparent").attr("src", iframeSrc); // reposition, load iframe by setting src
});

IE9 or jquery framework needs to fix this issue.

IE9 或 jquery 框架需要解决这个问题。

回答by Ian Devlin

I'm having a similar issue, however the iframe is added to the page rather than removed from it. It still appears to have the same problems.

我有一个类似的问题,但是 iframe 被添加到页面而不是从中删除。它似乎仍然存在相同的问题。