对话框一般分为两种类型:
模态类型(modal)与非模态类型(modeless)。
所谓模态对话框,就是指除非采取有效的关闭手段,用户的鼠标焦点或者输入光标将一直停留在其上的对话框。
非模态对话框则不会强制此种特性,用户可以在当前对话框以及其他窗口间进行切换。本文介绍如何使用JavaScript语言来创建这两种类型的对话框、控制其大小和位置、改变其外观以及在对话框间的数据传递。(引用)
一、创建模态和非模态对话框
创建模态对话框:(会缓存最近一次页面的值,通过一些设置可绕过系统的判断)
reValue = window.showModalDialog(URL , [Argument] ,[ornaments]);
创建非模态对话框:(不会)
reValue = window.showModelessDialog(URL ,[Argument] ,[ornaments]);
· reValue :对于showModalDialog(),它表示被打开的对话框窗口设置的returnValue属性值。对于showModelessDialog(),它表示新窗口对象。
· Argument:这个参数可用于传递某种类型的数据到打开的对话框,数据可以是一个数值、字符串、数组或者一个对象类型。在新窗口中引用这个数值时,可通过新创建window对象的dialogArguments 属性。
· ornaments:用这个参数指定新窗口的外观。可选择的窗口属性有很多种,当有多种控制需求时,将相关内容用一个字符串连接起来,其间用分号隔开。以下是可选择的属性种类:
o dialogHeight: sHeight
o dialogLeft: sXpos
o dialogTop: sYpos
o dialogWidth: sWidth
o center: ( yes | no | 1 | 0 | on | off )
o dialogHide: ( yes | no | 1 | 0 | on | off )
o edge: ( sunken | raised )
o help: ( yes | no | 1 | 0 | on | off )
o resizable: ( yes | no | 1 | 0 | on | off )
o scroll: ( yes | no | 1 | 0 | on | off )
o status: ( yes | no | 1 | 0 | on | off )
二、控制对话框大小和位置
三、改变对话框外观
四、从Noname1.html页面传递数据到Noname2.html页面
传递对象类型数据
-------Noname1.html------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>模态窗口传递对象</TITLE>
<script type="text/javascript" src="verify.js"></script>
<script language="javascript">
var sColor="yyyy";
var sName="xmddl369";
function showModalWindow(){
window.showModalDialog('Noname2.html',window,'dialogWidth:400px;dialogHeight:400px');
//do.....关闭模态窗口后会继续执行此行代码
}
function showModellessWindow(){
window.showModelessDialog('Noname2.html',window,'dialogWidth:400px;dialogHeight:400px;edge:sunken');
}
//也可上面的window换成元素对象eg:tid
function update()
{
oColor.innerText = sColor;
}
document.write("sColor="+sColor+"<br>");
document.write("sName="+sName+"<br>");
</script>
</HEAD>
<BODY>
<form>
<input type="text" name="tid" id="tid" value="">
<input type="button" name="button" value="打开一个模态窗口" onclick="showModalWindow()">
<input type="button" name="button" value="打开一个非模态窗口" onclick="showModellessWindow()">
<BR>
<P>输入你最喜欢的颜色: <SPAN ID="oColor" STYLE="color:red;font-size:24">Yellow</SPAN></P>
</form>
</BODY>
</HTML>
-------Noname2.html------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script language="javascript">
function getInfoAndUpdate() {
var callerWindowObj = dialogArguments; //得到文档的引用
callerWindowObj.sColor = document.all("oEnterColor").value;
callerWindowObj.update();
}
function cancel() {
var callerWindowObj = dialogArguments;
callerWindowObj.sColor = "Yellow";
callerWindowObj.update(); //调用父窗口的方法
}
</script>
</HEAD>
<BODY>
<form>
输入你最喜欢的颜色:<INPUT type="text" name="oEnterColor" ID="oEnterColor"><BR><BR>
<INPUT VALUE="Apply" TYPE=button onclick="getInfoAndUpdate();">
<INPUT VALUE="Ok" TYPE=button onclick="getInfoAndUpdate();window.close();">
<INPUT VALUE="Cancel" TYPE=button onclick="cancel();window.close();">
</form>
</BODY>
</HTML>
传递数组引用类型数据
-------Noname1.html------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript" src="verify.js"></script>
<script language="javascript">
var fruit=new Array();
fruit[0]="苹果";
fruit[1]="桔子";
fruit[2]="梨";
fruit[3]="香蕉";
function showModalWindow(){
window.showModalDialog('Noname2.html',fruit,'dialogWidth:400px;dialogHeight:400px');
}
function showModellessWindow(){
window.showModelessDialog('Noname2.html',fruit,'dialogWidth:400px;dialogHeight:400px;edge:sunken');
}
</script>
</HEAD>
<BODY>
<form>
<input type="button" name="button" value="打开一个模态窗口" onclick="showModalWindow()">
<input type="button" name="button" value="打开一个非模态窗口" onclick="showModellessWindow()">
<BR>
</form>
</BODY>
</HTML>
-------Noname2.html------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JScript">
function deWrite(){
var myobject=document.getElementById("ifruit");
var myfruit=dialogArguments;
for(count=0;count<myfruit.length;count++){
var oOption = document.createElement("OPTION");
oOption.text=myfruit[count];
oOption.value=/count;
myobject.add(oOption);
}
}
</SCRIPT>
</HEAD>
<BODY>
<form>
<input type="button" name="button" value="click me" onclick="deWrite()">
<br>增加水果:
<SELECT ID="ifruit" NAME="ifruit">
</SELECT>
</form>
</BODY>
</HTML>
传递值类型数据
-------Noname1.html------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript" src="verify.js"></script>
<script language="javascript">
function showModalWindow(){
window.showModalDialog('Noname2.html',"xmddl",'dialogWidth:400px;dialogHeight:400px');
}
function showModellessWindow(){
window.showModelessDialog('Noname2.html',"xmddl369",'dialogWidth:400px;dialogHeight:400px;edge:sunken');
}
</script>
</HEAD>
<BODY>
<form>
<input type="button" name="button" value="打开一个模态窗口" onclick="showModalWindow()">
<input type="button" name="button" value="打开一个非模态窗口" onclick="showModellessWindow()">
<BR>
</form>
</BODY>
</HTML>
-------Noname2.html------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JScript">
function deWrite(){
var names=dialogArguments;//
document.write(names);
}
</SCRIPT>
</HEAD>
<BODY>
<form>
<input type="button" name="button" value="click me" onclick="deWrite()">
</form>
</BODY>
</HTML>