js中弹出窗口关闭获得选择返回值
本文章来介绍利用showModalDialog命令实现弹出模态窗页面,获得选择返回值,并关闭效果,同时也介绍很多种弹出窗口返回值的程序,有需要学习的朋友可参考。
vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures])
1、主页面,代码如下:
- <script type="text/javascript" language="javascript">
- function DialogCustomerSelection() {
- var dlgResult = window.showModalDialog("CustomerSelection.aspx", window, "dialogWidth:480px; dialogHeight:240px; status:0");
- if (dlgResult != null) {
- //alert(dlgResult);
- var txtCode = document.getElementById("<%=TxtCustomerCode.ClientID%>");
- txtCode.value = dlgResult;
- }
- }
- </script>
- //… …
- <label>客户编码:<asp:TextBox runat="server" ID="TxtCustomerCode" ToolTip="客户编码" /></label>
- <a href="javascript:DialogCustomerSelection();">选择客户</a>
2、弹出页面,代码如下:
- <base target="_self" />
- <script type="text/javascript" language="javascript">
- function ReturnDialogResult(result) {
- window.returnValue = result;
- window.opener = null;
- window.close();
- }
- </script>
- <tr>
- <td>编码<%=i*3%></td>
- <td>客户名称<%=i*4%></td>
- <td><%=i*123412%></td>
- <td><%=i*123423%></td>
- <td>联系人<%=i*7%></td>
- <td>域名<%=i*13%></td>
- <td><a href="javascript:ReturnDialogResult('<%=i*3%>');">选择</a></td>
- </tr>
另一种方法,直接使用window.open,代码如下:
- <script language="javascript">
- <!--
- //打开模式窗口
- function open1(){
- //设置模式窗口的一些状态值
- var windowStatus = "dialogWidth:260px;dialogHeight:180px;center:1;status:0;";
- //在模式窗口中打开的页面
- var url = "test.html";
- //将模式窗口返回的值临时保存
- var temp = showModalDialog(url,"",windowStatus);
- //将刚才保存的值赋给文本输入框returnValue
- document.all.returnValue.value = temp;
- }
- //打开无菜单窗口
- function open2(){
- //设置窗口的一些状态值
- var windowStatus = "left=380,top=200,width=260,height=200,resizable=0,scrollbars=0,menubar=no,status=0";
- //在窗口中打开的页面
- var url = "test1.html";
- window.open(url,"noMenuWindowName",windowStatus);
- }
- //打开全屏窗口
- function open3(){
- //设置窗口的一些状态值
- var windowStatus = "fullscreen = 1";
- //在窗口中打开的页面
- var url = "test2.html";
- window.open(url,"noMenuWindowName",windowStatus);
- }
- -->
- </script>
- <body>
- <input type="button" name="btn1" value="打开模式窗口" onClick="open1()">
- <br>
从模式窗口返回的值:
- <input type="text" id="returnValue" name="returnValue">
补充:window.opener 的用法
window.opener 的用法在一般的用法中,只是用来解决关闭窗口时不提示弹出窗口, 而对它更深层的了解一般比较少。其 实 window.opener是指调用window.open方法的窗口。
在工作中主要是用来解决部分提交的。这种跨页操作对工作是非常有帮助的。
如果你在主窗口打开了一个页面,并且希望主窗口刷新就用这个,打开页面的window.opener就相当于主窗口的window。
主窗口的刷新你可以用
window.opener.location.reload();
如果你用虚拟的目录:如struts的*.do会提示你重试,你可以改成这样 window.opener.yourformname.submit() 就好了.
在应用中有这样一个情况,在A窗口中打开B窗口,在B窗口中操作完以后关闭B窗口,同时自动刷新A窗口,代码如下:
- function closeWin(){hasClosed = true;
- window.opener.location="javascript:reloadPage();";
- window.close();}
- function window.onbeforeunload(){if(!hasClosed){window.opener.location="javascript:reloadPage();";}}
- </script>
上面的代码在关闭B窗口的时候会提示错误,说缺少Object,正确的代码如下:
- function closeWin(){hasClosed = true;
- window.opener.location="javascript:reloadPage();";
- window.opener=null;window.close();}
- function window.onbeforeunload(){if(!hasClosed){//如果已经执行了closeWin方法,则不执行本方法window.opener.location="javascript:reloadPage();";}}
- </script>
reloadPage方法如下:
- function reloadPage() {
- history.go(0);
- document.execCommand("refresh")
- document.location = document.location;document.location.reload();
- }
PS:由于需要支持正常关闭和强制关闭窗口时能捕捉到事件,用了全局变量hasClosed.
补充,在父窗口是frame的时候在刷新父窗口的时候会出现问题:
The page cannot be refreshed without resending the information.
后修改如下:
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
不需要执行自带的reload()方法,注意,不要再画蛇添足加上这一句:
window.opener.parent.document.frames.item('mainFrame').location.reload();