javascript window.open showModelWindow收集

本文详细介绍JavaScript中使用window.showModalDialog及window.open方法打开新窗口并实现数据交互的过程,包括参数传递、返回值处理及对话框控制。

方法一 模式窗体


主页面打开新窗口js 事件:

function ShowModelWindow() {
         var iTop = (window.screen.height - 600) / 2;
         var iLeft = (window.screen.width - 500) / 2;
         var reVal = window.showModalDialog('BasicType.aspx' ,
        'dialogHeight: 300px; dialogWidth: 100px;dialogTop: ' + iTop + '; dialogLeft: ' + iLeft + '; edge: Raised; center: Yes; help: Yes; resizable: no; status: Yes;scroll:no;');
         if (typeof (reVal) != 'undefined') { form1.txtScd_Type.value = reVal; }//接收回传value
     }

1.window.showModalDialog(url,args,dialogattrs)
参数说明:
url:弹出页面地址
agrs:主窗口传给对话框的参数,可以是任意类型(数组也可以)
dialogattrs:弹出窗口的样式参数
模式对话框用法:
主窗口:var value =window.showModalDialog('test.jsp',strs,'resizable:yes');
弹出框中通过window.returnValue来设置返回值,上面的value拿到的就是这个值,然后主窗口中可以对
这个值进行处理,实现交互处理
注:模式对话框的应用就在于它的返回值,可以返回简单字符窜,也可以返回数组,非模式对话框类似
 

新打开窗口确定按钮:

function ConfirmBack() {
            window.returnValue = "要返回的value";
            window.close();
        }

 

方法二 window.open

 

主页面打开新窗口js 事件:

 

function ShowModelWindow() {
            var iTop = (window.screen.availHeight - 510) / 2; //获得窗口的垂直位置;
            var iLeft = (window.screen.availWidth - 650) / 2; //获得窗口的水平位置;
            window.open('GetHsCodeInfo.aspx', '', 'height=510px, width=650px, top=' + iTop + ',left=' + iLeft + ', toolbar=no,menubar=no, scrollbars=yes, resizable=no,location=no, status=no')
        }

 

新打开窗口确定按钮:

function ConfirmBack(a, b, c) {
            window.opener.document.getElementById("txtid1").value = a;
            window.opener.document.getElementById("txtid2").value = b;
            window.close();
        }

-----------------

2。window.open:

复制代码 代码如下:
<script>
function show_child()
{
var child=window .open("child.html","child","height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
/* if(!child.closed)
{
if(!window .close())
{
var textValue = frm.txt.value; parent.frm0.txt0.value = textValue;
}
else
{
window .close();
child.close();
}
}*/
}
</script>
<a href="javascript:show_child();">打开子窗口</a>
<form name=frm0>
<input type="text" name="txt0" id="txt0"> //注意这里一定要写ID属性不然FF下取不到值
</form>

【子窗口】
复制代码 代码如下:
<script>
function choseItem()
{
var v="";
var check_item = document.frm.item;
for(i=0;i<check_item.length;i++)
{
if(check_item[i].checked)
{
v+=","+check_item[i].value;
}
document.frm.txt.value=v.replace(/^,{1}/,"");
}
}
function foo()
{
window .close();
window .opener.document.getElementById("txt0").value=document.getElementById("txt").value
}
</script>
<body>
<form name=frm>
<input type=checkbox name=item value=1 onclick="choseItem();">a
<input type=checkbox name=item value=2 onclick="choseItem();">b
<input type=checkbox name=item value=3 onclick="choseItem();">c
<input type=checkbox name=item value=4 onclick="choseItem();">d
<input type=text name="txt" id="txt">
</form>
<input type=button value="关闭" onclick="foo();">
</body>

--------JS中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
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>
          var obj = new Object();
          obj.name="51js";
          window.showModalDialog("modal.htm",obj,"dialogWidth=200px;dialogHeight=100px");
</script>
modal.htm
<script>
          var obj = window.dialogArguments
          alert("您传递的参数为:" + obj.name)
</script>
-------------------------------
2.可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象。例如:
------------------------------
parent.htm
<script>
          str =window.showModalDialog("modal.htm",,"dialogWidth=200px;dialogHeight=100px");
          alert(str);
</script>
modal.htm
<script>
          window.returnValue="http://homepage.yesky.com";
</script>

常见技巧:

一、怎样才让在showModalDialog和showModelessDialog的超连接不弹出新窗口?
  在被打开的网页里加上<base target="_self">就可以了。这句话一般是放在<head>之间的。

二、怎样才刷新showModalDialog和showModelessDialog里的内容?
  在showModalDialog和showModelessDialog里是不能按F5刷新的,又不能弹出菜单。这个只能依靠

javascript了,以下是相关代码:

<body onkeydown="if (event.keyCode==116){reload.click()}">
<a id="reload" href="filename.htm" style="display:none">reload...</a>

  将filename.htm替换成网页的名字然后将它放到你打开的网页里,按F5就可以刷新了,注意,这个要

配合<base target="_self">使用,不然你按下F5会弹出新窗口的。

三、如何用javascript关掉showModalDialog(或showModelessDialog)打开的窗口。
  <input type="button" value="关闭" onclick="window.close()">
  也要配合<base target="_self">,不然会打开一个新的IE窗口,然后再关掉的。

四、Math.random与showModalDialog。

   当你设置的弹出网页固定时(如上面的"modal.htm"页面),ie很可能到临时文件区,下载上次产生的该页面(openPage.html),而没有重新加载,

   对于动态加载的页面来说,这样往往产生误会,如没有及时更新数据,也就更不利于开发者测试。所以,你可以采用如下方式:

      var strPage = “/medal.htm?random="+Math.random();

   这样每次产生的strPage是不一样的,原因也就不言自明了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值