javascript 父子窗口傳值示例代碼

javascript showModalDialog傳值與FireFox的window.open 父子窗口傳值示例代碼。先簡單介紹一下基本知識:
一、window.open()支持環境: Java1.0+/J1.0+/Nav2+/IE3+/Opera3+
二、基本語法:
window.open(pageURL,name,parameters)
其中:
pageURL 爲子窗口路徑
name 爲子窗口句柄
parameters 爲窗口參數(各參數用逗號分隔)
三、各項參數
其中yes/no也可使用1/0;pixel value爲具體的數值,單位象素。
參數 | 取值範圍 | 說明
alwaysLowered | yes/no | 指定窗口隱藏在所有窗口之後
alwaysRaised | yes/no | 指定窗口懸浮在所有窗口之上
depended | yes/no | 是否和父窗口同時關閉
directories | yes/no | Nav2和3的目錄欄是否可見
height | pixel value | 窗口高度
hotkeys | yes/no | 在沒菜單欄的窗口中設安全退出熱鍵
innerHeight | pixel value | 窗口中文檔的像素高度
innerWidth | pixel value | 窗口中文檔的像素寬度
location | yes/no | 位置欄是否可見
menubar | yes/no | 菜單欄是否可見
outerHeight | pixel value | 設定窗口(包括裝飾邊框)的像素高度
outerWidth | pixel value | 設定窗口(包括裝飾邊框)的像素寬度
resizable | yes/no | 窗口大小是否可調整
screenX | pixel value | 窗口距屏幕左邊界的像素長度
screenY | pixel value | 窗口距屏幕上邊界的像素長度
scrollbars | yes/no | 窗口是否可有滾動欄
titlebar | yes/no | 窗口題目欄是否可見
toolbar | yes/no | 窗口工具欄是否可見
Width | pixel value | 窗口的像素寬度
z-look | yes/no | 窗口被激活後是否浮在其它窗口之上
window.showModalDialog使用手冊
基本介紹:
showModalDialog() (IE 4+ 支持)
showModelessDialog() (IE 5+ 支持)
window.showModalDialog()方法用來創建一個顯示HTML內容的模態對話框。
window.showModelessDialog()方法用來創建一個顯示HTML內容的非模態對話框。
使用方法:
vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures])
vReturnValue = window.showModelessDialog(sURL [, vArguments] [,sFeatures])
參數說明:
sURL--
必選參數,類型:字符串。用來指定對話框要顯示的文檔的URL。
vArguments--
可選參數,類型:變體。用來向對話框傳遞參數。傳遞的參數類型不限,包括數組等。對話框通過window.dialogArguments來取得傳遞進來的參數。
sFeatures--
可選參數,類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開。
1.dialogHeight :對話框高度,不小於100px,IE4中dialogHeight 和 dialogWidth 默認的單位是em,而IE5中是px,爲方便其見,在定義modal方式的對話框時,用px做單位。
2.dialogWidth: 對話框寬度。
3.dialogLeft: 離屏幕左的距離。
4.dialogTop: 離屏幕上的距離。
5.center: {yes | no | 1 | 0 }:窗口是否居中,默認yes,但仍可以指定高度和寬度。
6.help: {yes | no | 1 | 0 }:是否顯示幫助按鈕,默認yes。
7.resizable: {yes | no | 1 | 0 } [IE5+]:是否可被改變大小。默認no。
8.status: {yes | no | 1 | 0 } [IE5+]:是否顯示狀態欄。默認爲yes[ Modeless]或no[Modal]。
9.scroll:{ yes | no | 1 | 0 | on | off }:指明對話框是否顯示滾動條。默認爲yes。
下面幾個屬性是用在HTA中的,在一般的網頁中一般不使用。
10.dialogHide:{ yes | no | 1 | 0 | on | off }:在打印或者打印預覽時對話框是否隱藏。默認爲no。
11.edge:{ sunken | raised }:指明對話框的邊框樣式。默認爲raised。
12.unadorned:{ yes | no | 1 | 0 | on | off }:默認爲no。
參數傳遞:
1.要想對話框傳遞參數,是通過vArguments來進行傳遞的。類型不限制,對於字符串類型,最大爲4096個字符。也可以傳遞對象,例如:
-------------------------------
parent.htm
複製代碼 代碼如下:
下面是網上的朋友發佈一篇測試代碼,大家可以測試下。
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>主頁面</title>
</head>
<script type="text/javascript"><!--
//傳數組
function check(){
var mxh1 = new Array("mxh","net_lover","孟子E章")
window.showModalDialog("test.html",mxh1,"unadorned:0;scroll:0;status:false;dialogWidth:380px;dialogHeight:200px");
}
//傳對象
function check1(){
var obj = new Object();
obj.name="zhangsan";
obj.age=2;
obj.sex="男";
window.showModalDialog("aaa.html",obj,"unadorned:0;scroll:0;status:false;dialogWidth:380px;dialogHeight:200px");
}
// --></script>
<body onload="check1();">
</body>
</html>

test.html 源代碼:
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>獲得主頁面的值</title>
</head>
<body>
script type="text/javascript"><!--
//傳數組方式
//var test = dialogArguments;
//alert(test[0]);
//alert(test[1]);
//alert(test[2]);
//傳對象方式
var obj = dialogArguments;
alert(obj.name);
alert(obj.age);
alert(obj.sex);

// --></script>
<input type="text" />
</body>
</html>

showModalDialog 傳值及刷新
showModalDialog使用例子,父窗口向子窗口傳遞值,子窗口設置父窗口的值,子窗口關閉的時候返回值到父窗口.
farther.html
複製代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document </TITLE>
<META content="EditPlus" name="Generator">
<META content="" name="Author">
<META content="" name="Keywords">
<META content="" name="Description">
<script language="javascript">
<!--
function openChild(){
var k = window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px");
if(k != null)
document.getElementById("txt11").value = k;
}
//-->
</script>
</HEAD>
<BODY>
<FONT face="宋體"></FONT>
<br>
傳遞到父窗口的值:<input id="txt9" type="text" value="3333333333333" name="txt9"><br>
返回的值:<input id="txt11" type="text" name="txt11"><br>
子窗口設置的值:<input id="txt10" type="text" name="txt10"><br>
<input id="Button1" onclick="openChild()" type="button" value="openChild" name="Button1">
</BODY>
</HTML>

child.html
複製代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document </TITLE>
<META content="EditPlus" name="Generator">
<META content="" name="Author">
<META content="" name="Keywords">
<META content="" name="Description">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
</HEAD>
<BODY>
<FONT face="宋體"></FONT>
<br>
父窗口傳遞來的值:<input id="txt0" type="text" name="txt0"><br>
輸入要設置父窗口的值:<input id="txt1" type="text" name="txt1"><input id="Button1" onclick="setFather()" type="button" value="設置父窗口的值" name="Button1"><br>
輸入返回的值:<input id="txt2" type="text" name="txt2"><input id="Button2" onclick="retrunValue()" type="button" value="關閉切返回值" name="Button2">
<input id="Button3" onclick="" type="button" value="關閉刷新父窗口" name="Button3">
<script language="javascript">
<!--
var k=window.dialogArguments;
//獲得父窗口傳遞來的值
if(k!=null)
{
document.getElementById("txt0").value = k.document.getElementById("txt9").value;
}
//設置父窗口的值
function setFather()
{
k.document.getElementById("txt10").value = document.getElementById("txt1").value
}
//設置返回到父窗口的值
function retrunValue()
{
var s = document.getElementById("txt2").value;
window.returnValue=s;
window.close();
}
//-->
</script>
</BODY>
</HTML>

說明:
由於showModalDialog緩存嚴重,下面是在子窗口取消客戶端緩存的設置.也可以在服務器端取消緩存,參考:
IT學習網的下一篇文章。
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
(二)下面是關閉刷新父窗口的例子
farther.html
複製代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="javascript">
<!--
function openChild()
{
var k = window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px");
if(k == 1)//判斷是否刷新
{
alert('刷新');
window.location.reload();
}
}
//-->
</script>
</HEAD>
<BODY>
<br>
傳遞到父窗口的值:<input id="txt9" type="text" value="3333333333333" NAME="txt9"><br>
<input type="button" value="openChild" onclick="openChild()" ID="Button1" NAME="Button1">
</BODY>
</HTML>

child.html
複製代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document </TITLE>
<META content="EditPlus" name="Generator">
<META content="" name="Author">
<META content="" name="Keywords">
<META content="" name="Description">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
</HEAD>
<BODY>
<FONT face="宋體"></FONT>
<br>
父窗口傳遞來的值:<input id="txt0" type="text" name="txt0"><br>
<input id="Button1" onclick="winClose(1)" type="button" value="關閉刷新父窗口" name="Button1">
<input id="Button2" onclick="winClose(0)" type="button" value="關閉不刷新父窗口" name="Button2">
<script language="javascript">
<!--
var k=window.dialogArguments;
//獲得父窗口傳遞來的值
if(k!=null)
{
document.getElementById("txt0").value = k.document.getElementById("txt9").value;
}
//關閉窗口返回是否刷新的參數.
function winClose(isRefrash)
{
window.returnValue=isRefrash;
window.close();
}
//-->
</script>
</BODY>
</HTML>

說明
1.下面是取消客戶端緩存的:
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
也可以在服務器端取消緩存,參考IT學習網下一篇文章
2.向父窗口傳遞闡述在ASP.NET中也可以是用aaa.aspx?id=1的方式傳遞.
3.不刷新父窗口的話在父窗口中直接這樣一來設置可以.
<script>
window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px");
</script>
4.在子窗口中若要提交頁面的話要加入:,這樣就不會打開新窗口了.
<head>
<base target="_self">
</HEAD>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章