javascript - 由不再存在的框架创建的 xml 文档上的 IE9 "permission denied"

标签 javascript internet-explorer-9 frames xmldocument permission-denied

我遇到了一个非常奇怪的情况。我有以下设置:

父窗口有一个对象“bar”,它包括一个 xml 文档和一些其他属性:

<html>
<head>
<script type="text/javascript">
var foo=null; //used in most report pages
var bar=function(document, boo){
    this.doc=document;
    this.boo=boo;
    this.baz="serge";
};
</script>
<head>  
<body>
    <iframe src="_child1.html"></iframe>
</body>
</html>

第一个子框架通过 ajax 加载一个简单的 xml 文件:

<html>
<head>
<script src="../lib/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
        url: "books.xml",
        type: "get",
        dataType: "xml",        
        success: function(data) {
            var boo = {
                name: "boo"
            };
            parent.foo = new parent.bar(data, boo);         
            alert(parent.foo.boo.name);
            alert(parent.foo.baz);
            alert(parent.foo.doc.firstChild.nodeName);
            window.location = "_child2.html";
        }
    });
</script>
</head>

<body>
    child1
</body>
</html>

XML 是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
    Vaidyanathan Nagarajan
</bookstore>

成功后,它会在父窗口中创建一个“bar”实例并将其存储在再次驻留在父窗口中的“foo”变量中。

然后它(成功)输出这些属性并重定向到第二个子框架。

第二个子框架尝试访问父框架中的“foo”对象并输出其属性:

<html>
<head>
<script src="../lib/jquery.min.js"></script>
<script type="text/javascript">
alert(parent.foo.boo.name);
alert(parent.foo.baz);
alert(parent.foo.doc.firstChild.nodeName);
</script>
</head>

<body>
    child2
</body>
</html>

现在有趣的事情发生了: 在 FF 和 Chrome 中,我们看到 3 个属性触发了两次:一次在 _child1 中,第二次在 _child2 中。然而在 IE9 中,我们只看到 5 个警告!

原因是 parent.foo.doc 出于某种原因抛出“Permission denied”。

我认为原因是 IE 以某种方式“记住”xml 文档是在第一个子框架中创建的(可能出于性能原因),而当第二个子框架试图访问该对象时 IE 无法找到它(或将其存储在受限的内存堆栈中)。

此外,当我在没有重定向的情况下尝试这种情况时——只是将父框架中的这两个子框架放在一起——第二个子框架在访问父框架的 xml 文档时没有问题。

一种解决方法是在分配之前将那些 xml 文档克隆到“父级”中,但我们有很多这样的场景,首先我想问问是否有人有更好的主意?

谢谢。

最佳答案

Internet Explorer 绝对不会允许您使用分配在单独页面中的 JavaScript 对象,一旦该单独页面被垃圾回收。您可以传递原始值,但我在帧之间传递“日期”实例时遇到过问题(很久很久以前,但仍然如此)。

您可以使用的一个技巧是调用父页面中的一个函数以从原始值创建您的对象:

// in parent:

function newBar(what, ever) {
  return new bar(what, ever);
}

// in the frame page:

parent.foo = parent.newBar("what", "ever");

认为这将使您远离问题,因为对象将通过父页面上的对象构造函数分配,而不是框架页面中的对象构造函数。在以前的生活中,我开发的应用程序是一个庞大的东西,涉及不计其数的 iframe(用于对话框、警告和各种各样的东西)。我一直遇到这个问题,保持事物清洁非常痛苦,以至于我发誓不再将 iframe 用于此类事物。

关于javascript - 由不再存在的框架创建的 xml 文档上的 IE9 "permission denied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6724787/

相关文章:

Delphi:框架之间的通信

javascript - 检测 javascript 是否在框架中执行

ffmpegX 从视频中提取帧

javascript - Jquery 更改 ajaxSource 变量

javascript - 如何在函数签名中使用 'this' 关键字来设置默认参数?

javascript - 是否可以在 window.location 加载新 URL 后调用函数?

angular - 在 IE9 中使用 Angular 2 需要什么?

javascript - 重复使用 HTML5 音频对象重复播放相同的 mp3 音效

internet-explorer-9 - 使用 CSS 在 IE9 上模拟雕刻(文本阴影)

javascript - IE9 3d曲面图