Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问同一域上IE8中嵌套帧的location.href?_Javascript_Internet Explorer_Iframe_Internet Explorer 8_Frameset - Fatal编程技术网

Javascript 如何访问同一域上IE8中嵌套帧的location.href?

Javascript 如何访问同一域上IE8中嵌套帧的location.href?,javascript,internet-explorer,iframe,internet-explorer-8,frameset,Javascript,Internet Explorer,Iframe,Internet Explorer 8,Frameset,我的主页包含一个,它指向一个包含的HTML文件。它看起来类似于以下内容(为了可读性而简化): 内容如下所示: <html> <body> <frameset> <frame name="menu" src="/same/domain/menu/url" /> <frame name="main" src="/same/domain/initial/main/url" /> </frameset>

我的主页包含一个
,它指向一个包含
的HTML文件。它看起来类似于以下内容(为了可读性而简化):


内容如下所示:

<html>
<body>
  <frameset>
    <frame name="menu" src="/same/domain/menu/url" />
    <frame name="main" src="/same/domain/initial/main/url" />
  </frameset>
</body>
</html>
不幸的是,在IE8上,它给了我“权限被拒绝”的错误。这看起来像是跨域脚本编写预防机制,但所有URL都来自同一域/协议/端口。对于相同的javascript代码,Chrome为我提供了正确的值(惊喜,惊喜)

请注意:

  • 我不能使用框架的
    src
    属性,因为用户可能已经使用“菜单”框架导航到另一个页面(仍然是同一个域)
  • 我无法控制
    iframe
    页面的内容,这些页面由应用程序的另一部分提供,从我的角度来看是不可修改的

如何避免这种情况?

一种方法是使用
postMessage
,它允许在不同的窗口/帧之间传递消息

在根窗口上,侦听消息

window.attachEvent("onmessage", (e) => {

    // handle message

});
将消息发布到子帧(
iframe
是dom节点)

在孩子体内

window.parent.postMessage({}, "*");
这允许一个简单的事件驱动通信方案,在该方案中,您以消息的形式发送操作,然后接收响应 作为消息上的
事件

在你的情况下,你会有一些东西:

// within child iframe
window.attachEvent("message", function (e) {
    // IE8 does not support object passing, only strings
    var message = JSON.parse(e.data);

    // wait for a GET_HREF message
    // and respond to it with the
    // data.
    if (message.type === "GET_HREF") {
        window.parent.postMessage(JSON.stringify({
            type: "GET_HREF",
            data: $("frame")
                .map(function () {
                    return this.href;
                })
                .get()
        }));
    }
});


// within parent window
window.attachEvent("message", function (e) {
    // IE8 does not support object passing, only strings
    var message = JSON.parse(e.data);

    // wait for a GET_HREF message
    if (message.type === "GET_HREF") {
        updateHref(message.data);
    }
});

iframe.contentWindow.postMessage(JSON.stringify({
    type: "GET_HREF"
}), "*");

谢谢你的解决方案。不幸的是,我无法修改
iframe
的内容,因此
frame
无法向父级发送消息。我已经相应地更新了这个问题。好吧,那么这可能有点难以实现。这样做的一种黑客方法可能是获取页面,将其解析为文档并查找帧的值?
window.parent.postMessage({}, "*");
// within child iframe
window.attachEvent("message", function (e) {
    // IE8 does not support object passing, only strings
    var message = JSON.parse(e.data);

    // wait for a GET_HREF message
    // and respond to it with the
    // data.
    if (message.type === "GET_HREF") {
        window.parent.postMessage(JSON.stringify({
            type: "GET_HREF",
            data: $("frame")
                .map(function () {
                    return this.href;
                })
                .get()
        }));
    }
});


// within parent window
window.attachEvent("message", function (e) {
    // IE8 does not support object passing, only strings
    var message = JSON.parse(e.data);

    // wait for a GET_HREF message
    if (message.type === "GET_HREF") {
        updateHref(message.data);
    }
});

iframe.contentWindow.postMessage(JSON.stringify({
    type: "GET_HREF"
}), "*");