4

我想從一個模式彈出窗口對我的父類進行簡單的調用,但IE瀏覽器一直在打我。任何建議來解決它將不勝感激。無法從IE7調用window.parent?

下面是我嘗試使用的精簡代碼。它在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> 
+0

如果您發現有幫助的話,請不要忘記接受答案,如果您想澄清,請發表評論。 –

回答

0

window.opener爲與window.open()打開彈出窗口使用。

您確定parent.html是topFrame1.html的父級,並且您並未暗中使用框架集?

0

IE模式對話框不是一個真正的窗口,不支持window.opener。要引用父窗口,你將不得不在對話框參數,像這樣的部分通過窗口參考:

window.showModalDialog("topFrame1.html",["name", window], 
    "dialogWidth:255px;dialogHeight:250px"); 

那麼你可以參考家長在孩子這一行:

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

我的建議是遠離IE對話框,只使用適用於所有瀏覽器的解決方案,例如window.open()jQuery dialogs