无法从IE7中调用window.parent?

4
我正在尝试从模态弹出窗口向我的父类进行简单的调用,但IE一直在反抗我。有什么建议可以解决这个问题将不胜感激。
以下是我尝试使用的简化代码。它在FireFox中运行得非常好,但在IE中会抛出一个错误 - “对象不支持此属性或方法”,引用“Catch”块中的代码行。这意味着Try和Catch块中的代码都无法工作。 parent.html
<html><head>
<script>
function callMain(msg)
{
    alert(msg);
}

function modalWin() {
    if (window.showModalDialog) {
        window.showModalDialog("topFrame1.html","name",
        "dialogWidth:255px;dialogHeight:250px");
    } else {
        window.open('topFrame1.html','name',
        'toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes');
    }
}

function getMainFrameVal()
{
    return document.getElementById("mainframe").value;
}

</script>
</head> <body>
<a href="#" onclick="modalWin()" >PopUpWindow</a>
<form>
<input type=text id="mainframe" value="main"/>
</body></html>

topFrame1.html

<html><head>
<script type="text/javascript">
function getMain(){
try{
    alert("1 "+ window.opener.getMainFrameVal());
}catch(e)
{
    alert("2 " +window.parent.getMainFrameVal());
}
}
</script>
</head> <body>
TOP <a href="#" onclick="getMain()">click for main</a> <br/><br/>
</body></html>

不要忘记接受有帮助的答案,或者如果您需要澄清,请发表评论。 - Drew Gaynor
2个回答

0

window.opener 用于使用 window.open() 打开弹出窗口。

你确定 parent.html 是 topFrame1.html 的父级,而不是在暗中使用 frameset 或其他东西吗?


0
IE 模态对话框并不是真正的窗口,不支持 window.opener。为了引用父窗口,你需要将窗口引用作为对话框参数传递进来,像这样:
    window.showModalDialog("topFrame1.html",["name", window],
    "dialogWidth:255px;dialogHeight:250px");

然后你可以使用这行代码在子元素中引用父元素:

    alert("1 "+ window.dialogArguments[1].getMainFrameVal());

我的建议是完全避免使用IE对话框,而是使用适用于所有浏览器的一种解决方案,例如window.open()jQuery对话框


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接