JavaScript子窗口ModalDialog中操作父窗口對像
更新時間:2012年12月11日 17:05:20 作者:
在使用js中會碰到這樣的需求:利用子窗口操作父窗口對像,本人很是遺憾,于是搜索整理下,拿出來和大家分享,需要的朋友可以參考下
在ModalDialog中操作父窗口對象
1、不能使用window.parent
Window.parent是用來在frame中進行操作的,在對話框中不能用來操作父窗口對象
2、正確的做法
調(diào)用modaldialog時通過傳參數(shù)的方式操作
例:
需求
父窗口頁面為a.html 子窗口頁面為b.html。a.html中有文本框id為test1,在打開的對話框中點擊按鈕,將a.html的文本框值改為“子窗口值”。
實現(xiàn)
打開對話框時把test1作為參數(shù)傳給子窗口,在子窗口中獲取參數(shù),將參數(shù)對象(即a.html中傳過來的text對象)的value屬性值設(shè)置為“子窗口值”
注意:這里只能傳id,不能傳name
a.html代碼如下
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<title>a.html</title>
</head>
<body>
<input type=text id=test1 value=”>
<input type=button value=” OK ” onclick='window.showModalDialog(“b.html”, test1)'>
</body>
</html>
b.html代碼如下
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<title>b.html</title>
<script language=javascript>
function func1(){
//獲取父窗口傳過來的參數(shù)
var ptextid = window.dialogArguments;
if(ptextid != undefined){
//將父窗口傳過來的對象的值改為“子窗口值”
ptextid.value = ”子窗口值“;
//關(guān)閉子窗口
window.close();
}
}
</script>
</head>
<body>
<input type=button value=” OK ” onclick=func1()>
</body>
</html>
如果需要操作的父窗口對象比較多,也可以將window或window.document作為參數(shù)傳給子窗口。
例:
需求
a.html中添加id為“aform”的的form,form中有id為test2的文本框,在b.html中,除了進行上面的操作之外,還要將test2的值改為“子窗口值2”,并將form提交到c.html。
實現(xiàn)1
將a.html中打開對話框的函數(shù)改為如下方式:
window.showModalDialog(“b.html”, window.document);
將b.html中func1()改為如下:
function func1(){
var pdoc = window.dialogArguments;
if(pdoc!=undefined){
pdoc.all.test1.value=”子窗口值“;
pdoc.all.test2.value=”子窗口值2″;
pdoc.all.aform.action=”c.html”;
pdoc.all.aform.submit();
}
}
實現(xiàn)2
因為在子窗口中對父窗口進行的操作比較多,也可以采用execScript的方式實現(xiàn)。
將a.html中打開對話框的函數(shù)改為如下方式:
window.showModalDialog(“b.html”, window);
添加javascript函數(shù)如下
function func(){
test1.value=”子窗口值“;
document.all.test2.value=”子窗口值2″;
aform.action=”c.html”;
aform.submit();
}
將b.html中func1()改為如下:
function func1(){
var pwin = window.dialogArguments;
if(pwin!=undefined){
var codeStr = ”func();”
pwin.execScript(codeStr,”javascript”);
window.close();
}
}
showModalDialog和showModelessDialog使用心得-轉(zhuǎn)載
一、showModalDialog和showModelessDialog有什么不同?
showModalDialog:被打開后就會始終保持輸入焦點。除非對話框被關(guān)閉,否則用戶無法切換到主窗口。類似alert的運行效果。
showModelessDialog:被打開后,用戶可以隨機切換輸入焦點。對主窗口沒有任何影響(最多是被擋住一下而以。 )
二、怎樣才讓在showModalDialog和showModelessDialog的超連接不彈出新窗口?
在被打開的網(wǎng)頁里加上<base target=”_self”>就可以了。這句話一般是放在<html>和<body>之間的。
三、怎樣才刷新showModalDialog和showModelessDialog里的內(nèi)容?
在showModalDialog和showModelessDialog里是不能按F5刷新的,又不能彈出菜單。這個只能依靠javascript了,以下是相關(guān)代碼:
<body onkeydown=”if (event.keyCode==116){reload.click()}”>
<a id=”reload” href=”filename.htm” style=”display:none”>reload…</a>
將filename.htm替換成網(wǎng)頁的名字然后將它放到你打開的網(wǎng)頁里,按F5就可以刷新了,注意,這個要配合<base target=”_self”>使用,不然你按下F5會彈出新窗口的。
四、如何用javascript關(guān)掉showModalDialog(或showModelessDialog)打開的窗口
<input type=”button” value=”關(guān)閉“ onclick=”window.close()”>
也要配合<base target=”_self”>,不然會打開一個新的IE窗口,然后再關(guān)掉的。
五、showModalDialog和showModelessDialog數(shù)據(jù)傳遞技巧
(作者語:本來想用一問一答形式來寫的,但是我想不出這個怎么問,所以只好這樣了。)
這個東西比較麻煩,我改了好幾次了不是沒辦法說明白(語文水平越來越差了),只好用個例子說明了。
例子:
現(xiàn)在需要在一個showModalDialog(或showModelessDialog)里讀取或設(shè)置一個變量var_name
一般的傳遞方式:
window.showModalDialog(“filename.htm”,var_name)
//傳遞var_name變量
在showModalDialog(或showModelessDialog)讀取和設(shè)置時:
alert(window.dialogArguments)//讀取var_name變量
window.dialogArguments=”oyiboy”//設(shè)置var_name變量
這種方式是可以滿足的,但是當你想在操作var_name同時再操作第二個變理var_id時呢?就無法再進行操作了。這就是這種傳遞方式的局限性。
以下是我建議使用的傳遞方式:
window.showModalDialog(“filename.htm”,window)
//不管要操作什么變量,只直傳遞主窗口的window對象
在showModalDialog(或showModelessDialog)讀取和設(shè)置時:
alert(window.dialogArguments.var_name)//讀取var_name變量
window.dialogArguments.var_name=”oyiboy”//設(shè)置var_name變量
同時我也可以操作var_id變量
alert(window.dialogArguments.var_id)//讀取var_id變量
window.dialogArguments.var_id=”001″//設(shè)置var_id變量
同樣還可以對主窗口的任何對象進行操作,如form對象里的元素。
window.dialogArguments.form1.index1.value=”這是在設(shè)置index1元素的值“
六、多個showModelessDialog的相互操作
因為光說很費勁,我就偷點懶,直接用代碼來說了,如果不明白的話那就直接來信(oyiboy#163.net(使用時請將#改成@))問我吧。
以下代碼的主要作用是在一個showModelessDialog里移動別一個showModelessDialog的位置。
主文件的部份js代碼。
var s1=showModelessDialog(‘控制.htm',window,”dialogTop:1px;dialogLeft:1px”) //打開控制窗口
var s2=showModelessDialog(‘a(chǎn)bout:blank',window,”dialogTop:200px;dialogLeft:300px”) //打開被控制窗口
控制.htm的部份代碼。
<script>
//操作位置數(shù)據(jù),因為窗口的位置數(shù)據(jù)是“xxxpx”方式的,所以需要這樣的一個特殊操作函數(shù)。
function countNumber(A_strNumber,A_strWhatdo)
{
A_strNumber=A_strNumber.replace(‘px',”)
A_strNumber-=0
switch(A_strWhatdo)
{
case ”-”:A_strNumber-=10;break;
case ”+”:A_strNumber+=10;break;
}
return A_strNumber + ”px”
}
</script>
<input type=”button” onclick=”window.dialogArguments.s2.dialogTop=countNumber(window.dialogArguments.s2.dialogTop,'-')” value=”上移“>
<input type=”button” onclick=”window.dialogArguments.s2.dialogLeft=countNumber(window.dialogArguments.s2.dialogLeft,'-')” value=”左移“>
<input type=”button” onclick=”window.dialogArguments.s2.dialogLeft=countNumber(window.dialogArguments.s2.dialogLeft,'+')” value=”右移“>
<input type=”button” onclick=”window.dialogArguments.s2.dialogTop=countNumber(window.dialogArguments.s2.dialogTop,'+')” value=”下移“>
以上關(guān)鍵部份是:
窗口命名方式:var s1=showModelessDialog(‘控制.htm',window,”dialogTop:1px;dialogLeft:1px”)
變量訪問方式:window.dialogArguments.s2.dialogTop
這個例子只是現(xiàn)實showModelessDialog與showModelessDialog之間的位置操作功能,通過這個原理,在showModelessDialog之間相互控制各自的顯示頁面,傳遞變量和數(shù)據(jù)等。這要看各位的發(fā)揮了
1、不能使用window.parent
Window.parent是用來在frame中進行操作的,在對話框中不能用來操作父窗口對象
2、正確的做法
調(diào)用modaldialog時通過傳參數(shù)的方式操作
例:
需求
父窗口頁面為a.html 子窗口頁面為b.html。a.html中有文本框id為test1,在打開的對話框中點擊按鈕,將a.html的文本框值改為“子窗口值”。
實現(xiàn)
打開對話框時把test1作為參數(shù)傳給子窗口,在子窗口中獲取參數(shù),將參數(shù)對象(即a.html中傳過來的text對象)的value屬性值設(shè)置為“子窗口值”
注意:這里只能傳id,不能傳name
a.html代碼如下
復(fù)制代碼 代碼如下:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<title>a.html</title>
</head>
<body>
<input type=text id=test1 value=”>
<input type=button value=” OK ” onclick='window.showModalDialog(“b.html”, test1)'>
</body>
</html>
b.html代碼如下
復(fù)制代碼 代碼如下:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<title>b.html</title>
<script language=javascript>
function func1(){
//獲取父窗口傳過來的參數(shù)
var ptextid = window.dialogArguments;
if(ptextid != undefined){
//將父窗口傳過來的對象的值改為“子窗口值”
ptextid.value = ”子窗口值“;
//關(guān)閉子窗口
window.close();
}
}
</script>
</head>
<body>
<input type=button value=” OK ” onclick=func1()>
</body>
</html>
如果需要操作的父窗口對象比較多,也可以將window或window.document作為參數(shù)傳給子窗口。
例:
需求
a.html中添加id為“aform”的的form,form中有id為test2的文本框,在b.html中,除了進行上面的操作之外,還要將test2的值改為“子窗口值2”,并將form提交到c.html。
實現(xiàn)1
將a.html中打開對話框的函數(shù)改為如下方式:
window.showModalDialog(“b.html”, window.document);
將b.html中func1()改為如下:
復(fù)制代碼 代碼如下:
function func1(){
var pdoc = window.dialogArguments;
if(pdoc!=undefined){
pdoc.all.test1.value=”子窗口值“;
pdoc.all.test2.value=”子窗口值2″;
pdoc.all.aform.action=”c.html”;
pdoc.all.aform.submit();
}
}
實現(xiàn)2
因為在子窗口中對父窗口進行的操作比較多,也可以采用execScript的方式實現(xiàn)。
將a.html中打開對話框的函數(shù)改為如下方式:
復(fù)制代碼 代碼如下:
window.showModalDialog(“b.html”, window);
添加javascript函數(shù)如下
復(fù)制代碼 代碼如下:
function func(){
test1.value=”子窗口值“;
document.all.test2.value=”子窗口值2″;
aform.action=”c.html”;
aform.submit();
}
將b.html中func1()改為如下:
復(fù)制代碼 代碼如下:
function func1(){
var pwin = window.dialogArguments;
if(pwin!=undefined){
var codeStr = ”func();”
pwin.execScript(codeStr,”javascript”);
window.close();
}
}
showModalDialog和showModelessDialog使用心得-轉(zhuǎn)載
一、showModalDialog和showModelessDialog有什么不同?
showModalDialog:被打開后就會始終保持輸入焦點。除非對話框被關(guān)閉,否則用戶無法切換到主窗口。類似alert的運行效果。
showModelessDialog:被打開后,用戶可以隨機切換輸入焦點。對主窗口沒有任何影響(最多是被擋住一下而以。 )
二、怎樣才讓在showModalDialog和showModelessDialog的超連接不彈出新窗口?
在被打開的網(wǎng)頁里加上<base target=”_self”>就可以了。這句話一般是放在<html>和<body>之間的。
三、怎樣才刷新showModalDialog和showModelessDialog里的內(nèi)容?
在showModalDialog和showModelessDialog里是不能按F5刷新的,又不能彈出菜單。這個只能依靠javascript了,以下是相關(guān)代碼:
<body onkeydown=”if (event.keyCode==116){reload.click()}”>
<a id=”reload” href=”filename.htm” style=”display:none”>reload…</a>
將filename.htm替換成網(wǎng)頁的名字然后將它放到你打開的網(wǎng)頁里,按F5就可以刷新了,注意,這個要配合<base target=”_self”>使用,不然你按下F5會彈出新窗口的。
四、如何用javascript關(guān)掉showModalDialog(或showModelessDialog)打開的窗口
<input type=”button” value=”關(guān)閉“ onclick=”window.close()”>
也要配合<base target=”_self”>,不然會打開一個新的IE窗口,然后再關(guān)掉的。
五、showModalDialog和showModelessDialog數(shù)據(jù)傳遞技巧
(作者語:本來想用一問一答形式來寫的,但是我想不出這個怎么問,所以只好這樣了。)
這個東西比較麻煩,我改了好幾次了不是沒辦法說明白(語文水平越來越差了),只好用個例子說明了。
例子:
現(xiàn)在需要在一個showModalDialog(或showModelessDialog)里讀取或設(shè)置一個變量var_name
一般的傳遞方式:
復(fù)制代碼 代碼如下:
window.showModalDialog(“filename.htm”,var_name)
//傳遞var_name變量
在showModalDialog(或showModelessDialog)讀取和設(shè)置時:
alert(window.dialogArguments)//讀取var_name變量
window.dialogArguments=”oyiboy”//設(shè)置var_name變量
這種方式是可以滿足的,但是當你想在操作var_name同時再操作第二個變理var_id時呢?就無法再進行操作了。這就是這種傳遞方式的局限性。
以下是我建議使用的傳遞方式:
window.showModalDialog(“filename.htm”,window)
//不管要操作什么變量,只直傳遞主窗口的window對象
在showModalDialog(或showModelessDialog)讀取和設(shè)置時:
alert(window.dialogArguments.var_name)//讀取var_name變量
window.dialogArguments.var_name=”oyiboy”//設(shè)置var_name變量
同時我也可以操作var_id變量
alert(window.dialogArguments.var_id)//讀取var_id變量
window.dialogArguments.var_id=”001″//設(shè)置var_id變量
同樣還可以對主窗口的任何對象進行操作,如form對象里的元素。
window.dialogArguments.form1.index1.value=”這是在設(shè)置index1元素的值“
六、多個showModelessDialog的相互操作
因為光說很費勁,我就偷點懶,直接用代碼來說了,如果不明白的話那就直接來信(oyiboy#163.net(使用時請將#改成@))問我吧。
以下代碼的主要作用是在一個showModelessDialog里移動別一個showModelessDialog的位置。
主文件的部份js代碼。
復(fù)制代碼 代碼如下:
var s1=showModelessDialog(‘控制.htm',window,”dialogTop:1px;dialogLeft:1px”) //打開控制窗口
var s2=showModelessDialog(‘a(chǎn)bout:blank',window,”dialogTop:200px;dialogLeft:300px”) //打開被控制窗口
控制.htm的部份代碼。
復(fù)制代碼 代碼如下:
<script>
//操作位置數(shù)據(jù),因為窗口的位置數(shù)據(jù)是“xxxpx”方式的,所以需要這樣的一個特殊操作函數(shù)。
function countNumber(A_strNumber,A_strWhatdo)
{
A_strNumber=A_strNumber.replace(‘px',”)
A_strNumber-=0
switch(A_strWhatdo)
{
case ”-”:A_strNumber-=10;break;
case ”+”:A_strNumber+=10;break;
}
return A_strNumber + ”px”
}
</script>
<input type=”button” onclick=”window.dialogArguments.s2.dialogTop=countNumber(window.dialogArguments.s2.dialogTop,'-')” value=”上移“>
<input type=”button” onclick=”window.dialogArguments.s2.dialogLeft=countNumber(window.dialogArguments.s2.dialogLeft,'-')” value=”左移“>
<input type=”button” onclick=”window.dialogArguments.s2.dialogLeft=countNumber(window.dialogArguments.s2.dialogLeft,'+')” value=”右移“>
<input type=”button” onclick=”window.dialogArguments.s2.dialogTop=countNumber(window.dialogArguments.s2.dialogTop,'+')” value=”下移“>
以上關(guān)鍵部份是:
窗口命名方式:var s1=showModelessDialog(‘控制.htm',window,”dialogTop:1px;dialogLeft:1px”)
變量訪問方式:window.dialogArguments.s2.dialogTop
這個例子只是現(xiàn)實showModelessDialog與showModelessDialog之間的位置操作功能,通過這個原理,在showModelessDialog之間相互控制各自的顯示頁面,傳遞變量和數(shù)據(jù)等。這要看各位的發(fā)揮了
您可能感興趣的文章:
- JavaScript子窗口調(diào)用父窗口變量和函數(shù)的方法
- JavaScript實現(xiàn)彈出子窗口并傳值給父窗口
- jquery、js調(diào)用iframe父窗口與子窗口元素的方法整理
- js父窗口關(guān)閉時子窗口隨之關(guān)閉完美解決方案
- js關(guān)閉父窗口時關(guān)閉子窗口
- js 父窗口控制子窗口的行為-打開,關(guān)閉,重定位,回復(fù)
- 用javascript實現(xiàn)始終保持打開同一個子窗口以及關(guān)閉父窗口同時自動關(guān)閉所有子窗口
- 用javascript父窗口控制只彈出一個子窗口
- JavaScript中的子窗口與父窗口的互相調(diào)用問題
相關(guān)文章
JavaScrip關(guān)于創(chuàng)建常量的知識點
這篇文章主要介紹了JavaScrip創(chuàng)建常量的相關(guān)知識點,幫助大家對JS更加深入的學習,參考下吧。2017-12-12ASP.NET實現(xiàn)Repeater控件的數(shù)據(jù)綁定
這篇文章介紹了ASP.NET實現(xiàn)Repeater控件數(shù)據(jù)綁定的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07addEventListener()第三個參數(shù)useCapture (Boolean)詳細解析
true的觸發(fā)順序總是在false之前;如果多個均為true,則外層的觸發(fā)先于內(nèi)層;如果多個均為false,則內(nèi)層的觸發(fā)先于外層2013-11-11