IE的showModalDialog与FireFox的window.open 父子窗口传值

转自:我爱互联网

先简单介绍一下基本知识:
一、window.open()支持环境:

 JavaScript1.0+/JScript1.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

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

modal.htm

<script type="text/javascript">
window.returnValue="http://www.51js.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的代码如下:

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 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 = color;
        oOption.value = color;
        //检查浏览器类型
        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>
知识共享许可协议
作品Tim Zhang创作,采用知识共享署名 3.0 中国大陆许可协议进行许可。 。
原文地址:https://www.cnblogs.com/ccdc/p/2095217.html