先簡單介紹一下基本知識:
一、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.gxlcms.com";
</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>
下面是網上的朋友發布一篇測試代碼,大家可以測試下。
代碼如下:
<!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緩存嚴重,下面是在子窗口取消客戶端緩存的設置.也可以在服務器端取消緩存,參考:
腳本之家的下一篇文章。
<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">
也可以在服務器端取消緩存,參考腳本之家下一篇文章
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>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com