本文摘自:
http://blog.dofy.net/?p=140
?
使用 window.showModalDialog 打開的窗口沒有 opener 對象, 這的確給使用帶來了很多麻煩, 以前一直也沒用過這個東西, 所以沒有更多關注. 今天一個朋友問到這個問題, 于是上網搜索了一下, 找到了解決方案, 發上來供大家參考.
首先來看看 window.showModalDialog 的參數:
vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures]) ;
?
sURL : 打開窗口的地址;
vArguments : 傳遞參數;
sFeatures : 窗口屬性列表;
第一個參數是必須的, 后兩個可以省略.
這里我們要利用的就是第二個參數. 原理是將父窗口的被控制對象以參數的形式傳遞到子窗口, 在子窗口中直接控制這個對象即可.
舉例來說:
parent.html
<script type="text/javascript">
function openWin(){
// 子窗口地址
var srcFile = "child.html";
// 子窗口屬性
var winFeatures = "dialogHeight:300px; dialogLeft:200px;";
// 將 From 的 ID 作為參數傳遞到子窗口
var obj = getForm;
// 打開子窗口
window.showModalDialog(srcFile, obj, winFeatures);
}
</script>
<form id="getForm">
??回傳值:
??<input type="text" id="getValue" readOnly>
</form>
<input type="button" value="打開" onClick="openWin()">
child.html
<script type="text/javascript">
function send(){
??// 獲取參數傳遞的對象
??var obj = window.dialogArguments;
??// 控制對象
??obj.getValue.value = 'from Child';
}
</script>
<a href="#" onclick="send();">Send</a>
運行 parent.html , 單擊 [打開] 按鈕彈出對話框, 點擊 Send 鏈接將值傳遞到 parent 的文本框中.
傳遞的參數當然也可以是其他對象, 例如 window . 值得注意的是傳遞的是對象, 而不是字符串.