window.open 与 window.showModalDialog比较

window.openwindow.showModalDialog都是用来打开子窗口的。

open形式是直接打开一个子窗口,但在ie7以及ie8上都不会直接弹出一个窗口,而是在新标签页中打开,这带来两个问题,一个是操作不方便,另一个是原本明明在open时指定了窗口大小的,但是由于是标签页打开,导致设置的窗口大小完全没用,页面中的内容也是在新窗口中完全打开的,本来设计很好的页面打开后完全变形了。这个时候最好是选用dialog形式打开窗口,使用它打开窗口时有几点需要注意的。

1设置窗口的参数:
   open设置参数一般以逗号分隔;
    dialog中则是以分号分隔的;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   window.open()参数:
   window.open ('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no,rollbars=no, resizable=no,
location=no, status=no') ;
     参数说明:
     window.open 弹出新窗口的命令; 
   'page.html' 弹出窗口的文件名; 
   'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替; 
   height=100 窗口高度; 
   width=400 窗口宽度; 
   top=0 窗口距离屏幕上方的象素值; 
   left=0 窗口距离屏幕左侧的象素值; 
   toolbar=no 是否显示工具栏,yes为显示; 
   menubarscrollbars 表示菜单栏和滚动栏。 
   resizable=no 是否允许改变窗口大小,yes为允许; 
   location=no 是否显示地址栏,yes为允许; 
   status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    window.showModalDialog(sURL,vArguments,sFeatures)参数:
   window.showModalDialog('page.html','','resizable:yes;scroll:yes;status:no;dialogWidth=320px;dialogHeight=230px;center=yes;
help=no');
    参数说明:   
   sURL           --  必选参数,类型:字符串。用来指定对话框要显示的文档的URL   
   vArguments-- 可选参数,类型:变体。用来向对话框传递参数。
                  传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。   
   sFeatures   -- 可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。   
   1.dialogHeight:对话框高度,不小于100px.
   2.dialogWidth: 对话框宽度。   
   3.dialogLeft:离屏幕左的距离。   
   4.dialogTop:离屏幕上的距离。   
   5.center: {yesno10}:窗口是否居中,默认yes,但仍可以指定高度和宽度。   
   6.help:    {yesno10}:是否显示帮助按钮,默认yes   
   7.resizable:{yesno10}[ie5+]:是否可被改变大小。默认no   
   8.status:    {yesno10}[IE5+]:是否显示状态栏。默认为yes[Modeless]no[Modal]   
   9.scroll:      {yesno10onoff}:指明对话框是否显示滚动条。默认为yes   
   下面几个属性是用在HTA中的,在一般的网页中一般不使用。   
   10.dialogHide:{yesno10onoff}:在打印或者打印预览时对话框是否隐藏。默认为no   
   11.edge:         {sunkenraised}:指明对话框的边框样式。默认为raised   
   12.unadorned:{yesno10onoff}:默认为no  

2如何访问父窗口中的元素:
     open 形式打开的子窗口,通过window.opener就可以了,如window.opener.user.value=啦啦啦。
     dialog形式打开的子窗口,在写window.showModalDialog时,将window作为该方法的第二个参数,然后在子窗口中通过window.dialogArguments就可以定位到父窗口的window对象。如var bb =  window.dialogArguments;bb.document.getElementById("user").value = 啦啦啦。
3例子:
问题一:我想弹出一个窗口,然后在弹出的窗口里,选择或输入一些信息,要求这些信息返回到父页面。
建立父页面:a.htm 

CODE:

<html>
<head>
    <title></title>
    <mce:script language="javascript" type="text/javascript"><!--
        function OpenWin(){
            var getv = showModalDialog("b.htm", "", "dialogWidth:320px; dialogHeight:200px;status:no;help:yes");
            if (getv != null){
                document.forms[0].txtName.value=getv.split(",")[0];
                document.forms[0].txtAge.value=getv.split(",")[1];
            }
        }
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <input type="text" name="txtName" id="txtName" />
    <input type="text" name="txtAge" id="txtAge" />
    <input type="button" name="Submit" value="打开" onclick="OpenWin()" />
    </form>
</body>
</html>建立子页面:b.htm 

CODE:

<html>
<head>
    <title></title>
    <mce:script language="javascript" type="text/javascript"><!--
        function GetValue(){
            //window.returnValue存放子窗口向父窗口所传值的内容
        window.returnValue=document.forms[0].txtName.value+","+document.forms[0].txtAge.value;
            window.close();
        }// --></mce:script>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <br />
    姓名:<input name="txtName" type="text" id="txtName" /><br />
    年龄:<input name="txtAge" type="text" id="txtAge" />
    <input type="button" name="Submit" value="关闭" onclick="GetValue()" />
    </form>
</body>
</html>这里利用了模式窗口window.showModalDialog(),利用window.returnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures]),我们可以打开一个模态窗口,该窗口的优点是限制用户只能对当前的页面进行操作,而对其父页面不能进行操作,常用于向导或者信息获取页面。利用其中的vArguments我们可以在父页面和弹出的页面中进行参数的传递,参数可以为自定义的对象,也可以传递父页面中任何一个控件的引用,这使得我们可以很容易的来操作父页面中的各个元素,使得参数的传递变得非常容易。
问题二、第2个参数的讨论
showModalDialog("子页面地址","向子页面传递参数","子页面外观设置"),主要研究下第2个参数。
1.第2个参数为自定义对象 
我们可以定义一个javascript对象,然后定义各种自定义属性的值,然后可以将此对象传递到子页面中。
例:将父页面的信息封装成一个对象,然后将该对象传递给子页面。 
父页面:oneParent.htm 

CODE:

<html>
<head>
    <title>父页面向子页面传递自定义对象参数</title>
    <mce:script language="javascript" type="text/javascript"><!--
        function OpenWin(){
            //发送页面,利用模式窗口的第2个参数向子页面传递一个对象
        var person=new Object();
            person.myName=document.forms[0].txtName.value;
            person.myAge=document.forms[0].txtAge.value;
            window.showModalDialog("oneSon.htm",person,""); //第2个参数是一个对象
     }
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server" method="post">
        姓名:<input type="text" name="txtName" id="txtName" />
        年龄:<input type="text" name="txtAge" id="txtAge" />
        <input type="button" name="Submit" value="打开" onclick="OpenWin()" />
    </form>
</body>
</html>子页面:oneSon.htm 

CODE:

<html>
<head>
    <title></title>
    <mce:script language="javascript" type="text/javascript"><!--
       window.onload=function(){
            //对话框通过window.dialogArguments来取得传递进来的参数。   
        var person=window.dialogArguments;
            document.forms[0].txtName.value=person.myName;
            document.forms[0].txtAge.value=person.myAge;
        };
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <br />
    姓名:<input name="txtName" type="text" id="txtName" /><br />
    年龄:<input name="txtAge" type="text" id="txtAge" />
    <input type="button" name="Submit" value="关闭" onclick="window.close()" />
    </form>
</body>
</html>以上代码中,文档全部加载完,才执行window.onload指定函数,以找到document.forms[0].txtName对象,如果去掉,window.onload=function(){}直接写大括号里的代码,则必须把这个JS代码块放到</body>之后才能正确运行,否则出现运行时间错误,找不到document.forms[0].txtName对象
2.第2个参数是父页面的一个元素 
我们可以将父页面中元素对象的引用传递给子页面,通过该引用我们可以访问父页面中的该元素对象。
例:其中利用元素对象的引用我们可以操纵父页面的元素对象的属性。
父页面:twoParent.htm 

CODE:

<html>
<head>
    <title>父页面向子页面传递父页面元素</title>
    <mce:script language="javascript" type="text/javascript"><!--
        function OpenWin(){
            //发送页面,利用模式窗口的第2个参数向子页面传递一个对象
        window.showModalDialog("twoSon.htm",parentDiv,""); //2个参数是父页面中的元素对象
     }    
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <div id="parentDiv">
        姓名:<input type="text" name="txtName" id="txtName" />
        年龄:<input type="text" name="txtAge" id="txtAge" />
        <input type="button"  value="打开" onclick="OpenWin()" />
    </div>
    </form>
</body>
</html>子页面:twoSon.htm 

CODE:

<html>
<head>
    <title></title>
    <mce:script language="javascript" type="text/javascript"><!--
//       window.onload=function(){
            //对话框通过window.dialogArguments来取得传递进来的参数。   
            var infoKeYi=window.dialogArguments;
//        };    
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server" >
    <div id="childDiv">
    </div>
    <br />
    <!-- 注释:显示传递进来的,父页面中的元素信息-->
    <input type="button" value="显示父页面传递的信息" onclick="childDiv.innerHTML=infoKeYi.innerHTML" /> 
    <input type="button" value="子页面操作父页面的信息", onclick='infoKeYi.style.backgroundColor="green"'/> 
    <!-- 注释:显示传递进来的父元素信息-->
    <input type="button" name="Submit" value="关闭" onclick="window.close()" />
    </form>
</body>
</html>这里注释掉window.onload=function(){},直接写大括号里的代码,目的让JS代码先加载,后加载<body>里的元素,否则出现找不到对象infoKeYi的错误。
.第2个参数是window.
如果第2个参数是window,这样可以取得父窗口的一些数据和方法。如:
//取得父窗口的JS变量 var
window.dialogArguments.父窗口中的变量;
//获得父窗口的对象和属性
window.dialogArguments.form1.父窗口中控件name.value ;
//调用父窗口的方法 fun
window.dialogArguments.父窗口中的方法 
父页面:threeParent.htm 

CODE:

<html>
<head>
    <title>父页面向子页面传递父页面元素</title>
    <mce:script language="javascript" type="text/javascript"><!--
        //打开窗口
     function OpenWin(){
            window.showModalDialog("threeSon.htm",window,"dialogWidth:300px;dialogHeight:200px;status:no"); //2个参数是window对象
     }
        //定义一个子窗口要调用的变量
     var age=20;
        //定义一个子窗口要调用的方法
     function getHello(){
            return "你好,世界!";
        }    
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <div id="parentDiv" align="center">
        姓名:<input type="text" name="txtName" id="txtName" />
        <input type="button"  value="打开" onclick="OpenWin()" />
    </div>
    </form>
</body>
</html>子页面:threeSon.htm 

CODE:

<html>
<head>
    <title></title>
    <mce:script language="javascript" type="text/javascript"><!--
       window.onload=function(){
            //对话框通过window.dialogArguments来取得传递进来的参数。   
        //获得父窗口的对象和属性
        document.forms[0].txtName.value=window.dialogArguments.form1.txtName.value;
            //取得父窗口的JS变量
        document.forms[0].txtAge.value=window.dialogArguments.age;
            //调用父窗口的方法 
        document.forms[0].txtSay.value=window.dialogArguments.getHello();
        };    
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server" >
    <div id="childDiv" align="center">
    </div>
    <br />
    <!-- 注释:显示传递进来的,父页面中的元素信息-->
    姓名:<input name="txtName" type="text" id="txtName" /><br />
    年龄:<input name="txtAge" type="text" id="txtAge" /><br />
    问候:<input name="txtSay" type="text" id="txtSay" /><br />
    <input type="button" name="Submit" value="关闭" onclick="window.close()" />
    </form>
</body>
</html>

360 doc

http://www.360doc.com/relevant/08/0123/18/3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值