javascript showModalDialog傳值與FireFox的window.open 父子視窗傳值樣本第1/2頁_javascript技巧

來源:互聯網
上載者:User
先簡單介紹一下基本知識:
一、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
複製代碼 代碼如下:

<script type="text/javascript">
var obj = new Object();
obj.name="51js";
window.showModalDialog("modal.htm",obj,"dialogWidth=200px;dialogHeight=100px");
</script>

modal.htm
複製代碼 代碼如下:

<script type="text/javascript">
var obj = window.dialogArguments
alert("您傳遞的參數為:" + obj.name)
</script>

-------------------------------
2.可以通過window.returnValue向開啟對話方塊的視窗返回資訊,當然也可以是對象。例如:
------------------------------
parent.htm
複製代碼 代碼如下:

< type="text/java">
str =window.showModalDialog("modal.htm",,"dialogWidth=200px;dialogHeight=100px");
alert(str);
</script>

modal.htm
複製代碼 代碼如下:

<script type="text/javascript">
window.returnValue="http://www.jb51.net";
</script>

在IE中,我們可以使用showModalDialog來傳值。
文法如下:
vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures])
但是.在Firefox中卻沒有showModalDialog方法,不過我們可以用window.open()
文法如下:
oNewWindow = window.open( [sURL] [, sName] [, sFeatures] [, bReplace])
只是,在Firefox下,window.open的參數中,sFeature多了一些功能設定,而在FireFox下要讓開啟的視窗跟IE的showModalDialog一樣的話,只要在sFeatures中加個modal=yes就可以了。
下面用一個樣本來說明其用法。
功能說明:從子視窗中輸入顏色種類提交到父視窗,並添加選項到下拉式清單。
a.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>a.html文檔</title>
<script language="javascript">
function openstr()
{
ReturnValue=window.showModalDialog("b.html",window,"dialogWidth=510px;dialogHeight=150px;status=no");
if(ReturnValue && ReturnValue!="")
{
oOption = document.createElement('OPTION');
oOption.text=ReturnValue;
oOption.value=ReturnValue;
document.all.txtselect.add(oOption);
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>
<select name="txtselect" id="txtselect">
</select>
</label>
<label>
<input type="button" name="Submit" value="開啟子視窗" onclick="openstr()" />
</label>
</form>
</body>
</html>

b.html
複製代碼 代碼如下:

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>b.html文檔</title>
<script language="javascript">
function ClickOk()
{
var t=document.Edit;
var url=t.color.value;
if(url==null||url=="填寫顏色") return(false);
window.returnValue=url;
window.close();
}
</script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="2" align="center" width="300">
<form name="Edit" id="Edit">
<tr>
<td width="30" align="right" height="30">color:</td>
<td height="30"><input type="text" name="color" value="填寫顏色" /></td>
<td width="56" align="center" height="30"><input " type="button" name="bntOk" value="確認" onclick="ClickOk();" /> </td>
</tr>
</form>
</table>
</body>
</html>
修改為相容IE和FireFoxr的代碼如下:
[code]
<!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>a.html文檔</title>
<script type="text/javascript">
function openstr()
{
window.open("b.html","","modal=yes,width=500,height=500,resizable=no,scrollbars=no");
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>
<select name="txtselect" id="txtselect">
</select>
</label>
<label>
<input type="button" name="Submit" value="開啟子視窗" onclick="openstr()" />
</label>
</form>
</body>
</html>

b.html
複製代碼 代碼如下:

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>b.html文檔</title>
<script type="text/javascript">
function ClickOk()
{
var t=document.Edit;
var color=t.color.value;
if(color==null||color=="填寫顏色") return(false);
var oOption = window.opener.document.createElement('OPTION');
oOption.text=url;
oOption.value=url;
//檢查瀏覽器類型
var bname = navigator.appName;
if (bname.search(/netscape/i) == 0)
{
window.opener.document.getElementById("txtselect").appendChild(oOption);
}
else if (bname.search(/microsoft/i) == 0)
{
window.opener.document.all.txtselect.add(oOption);
}
else
{
}
window.close();
}
</script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="2" align="center" width="300">
<form name="Edit" id="Edit">
<tr>
<td width="30" align="right" height="30">color:</td>
<td height="30"><input type="text" name="color" value="填寫顏色" /></td>
<td width="56" align="center" height="30"><input " type="button" name="bntOk" value="確認" onclick="ClickOk();" /> </td>
</tr>
</form>
</table>
</body>
</html>

當前1/2頁  12下一頁閱讀全文
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.