现在的位置: 首页 > 综合 > 正文

不同窗口间的传值

2018年06月10日 ⁄ 综合 ⁄ 共 4533字 ⁄ 字号 评论关闭

三种方法:
1.JS方法
在b中通过opener取得a窗口对象,如
a中:
var   a= "a ";
在b中,通过
opener.a得到值 "a ";

2.jsp
window.open( "b.jsp?param=a ");
在JSP中request.getParamter("param ")取得param的值;

3.  用showModalDialog
用法如下:
v ReturnValue   =   window.showModalDialog(sURL   [,  vArguments]   [,   sFeatures])

父窗口向子窗口   传入参数:
  要想对话框传递参数,是通过vArguments来进行传递的。类型不限制,对于字符串类型,最大为4096个字符。也可以传递对象,例如:
  test1.htm
  ====================
  <script>
    var   mxh1   =   new   Array( "mxh", "net_lover ", "孟子E章")
    var   mxh2   =   window.open( "about:blank", "window_mxh ")
    //   向对话框传递数组
    window.showModalDialog( "test2.htm ",mxh1)
    //   向对话框传递window对象
    window.showModalDialog( "test3.htm ",mxh2)
  </script>

  test2.htm
  ====================
  <script>
    var   a   =   window.dialogArguments
    alert( "您传递的参数为: "  +   a)  
  </script>

  test3.htm
  ====================
  <script>
    var   a   =   window.dialogArguments
    alert( "您传递的参数为window对象,名称:"   +   a.name)  
  </script>

子窗口向父窗口   传入参数:
  可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象。例如:
  test4.htm
  ===================
  <script>
    var   a   =   window.showModalDialog("test5.htm ")
    for(i=0;i <a.length;i++)   alert(a[i])  
  </script>

  test5.htm
  ===================

<html>

<head>
  <script>
  function   sendTo()
  {
    var   a=new   Array( "a ", "b ")
    window.returnValue   =   a  
    window.close()
  }
  </script>

</head>
  <body>
  <form>
    <input   value= "返回 "  type=button   onclick= "sendTo() ">
  </form>

</body>

</html>

 

 

----------------------------------------------------------------------------------------------------------------
Javascript怎么在两个窗体之间传值2-showModalDialog

2009-09-09 11:44:42

标签:

javascript中还有一个函数window.showModalDialog也可以打开一个新窗体,不过他打开的是一个模态窗口,那么如何在父窗体和子窗体之间传值呢?我们先看该函数的定义:vReturnValue = window.showModalDialog(sURL [, vArguments][,sFeatures])  
 

参数说明:  

sURL--必选参数,类型:字符串。用来指定对话框要显示的文档的URL。  

vArguments--可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。

sFeatures--可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。    

dialogHeight :对话框高度,不小于100px,IE4中dialogHeight 和 dialogWidth 默认的单位是em,而IE5中是px,为方便其见,在定义modal方式的对话框时,用px做单位。
dialogWidth: 对话框宽度。  
dialogLeft: 离屏幕左的距离。
dialogTop: 离屏幕上的距离。  
center: {yes | no | 1 | 0 }:窗口是否居中,默认yes,但仍可以指定高度和宽度。
help: {yes | no | 1 | 0 }:是否显示帮助按钮,默认yes。    
resizable: {yes | no | 1 | 0 } [IE5+]:是否可被改变大小。默认no。
status: {yes | no | 1 | 0 } [IE5+]:是否显示状态栏。默认为yes[Modeless]或no[Modal]。
scroll:{ yes | no | 1 | 0 | on | off }:指明对话框是否显示滚动条。默认为yes。
如:"dialogWidth=200px;dialogHeight=100px"
因此我们可以通过window.dialogArguments参数来在两个窗体之间传值
如下面两个页面:FatherPage.htm:
 XML-Code:
<script type="text/javascript">
function OpenChildWindow()
{
    window.showModalDialog('ChildPage.htm',document.getElementById('txtInput').value);
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" />

 

ChildPage.htm:

 XML-Code:
<body onload="Load()">
<script type="text/javascript">
function Load()
{
    document.getElementById('txtInput').value=window.dialogArguments;
}
</script>
<input type="text" id="txtInput" />
</body>
 

上面只是传递简单的字符串,我们还可以传递数组,如:FatherPage.htm:

 XML-Code:
<script type="text/javascript">
function OpenChildWindow()
{
    var args = new Array();
    args[0] = document.getElementById('txtInput').value;
    window.showModalDialog('ChildPage.htm',args);
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" />

 

ChildPage.htm:

 XML-Code:
<script type="text/javascript">
function Load()
{  
   document.getElementById('txtInput').value=window.dialogArguments[0] ;
}
</script>
 

同样我们还可以传递对象,如:FatherPage.htm:

 XML-Code:
<script type="text/javascript">
function OpenChildWindow()
{
   var obj = new Object();
   obj.name = document.getElementById('txtInput').value;
    window.showModalDialog('ChildPage.htm',obj);
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" />

 

ChildPage.html:

 XML-Code:
<script type="text/javascript">
function Load()
{  
    var obj = window.dialogArguments;
    document.getElementById('txtInput').value=obj.name ;
}
</script>
 

以上都是从父窗体向子窗体传值,那么如何从子窗体向父窗体传值呢 ?其实通过window.returnValue就可以获取子窗体的值,window.returnValue与window.dialogArguments一样,可以是任意变量,包括字符串,数组,对象等。如:FatherPage.html:

 XML-Code:
<script type="text/javascript">
function OpenChildWindow()
{
   var obj = new Object();
   obj.name = document.getElementById('txtInput').value;
   var result = window.showModalDialog('ChildPage.htm',obj);
   document.getElementById('txtInput').value = result.name;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" />

ChildPage.html:

 XML-Code:
<body onload="Load()">
<script type="text/javascript">
function Load()
{  
    var obj = window.dialogArguments;
    document.getElementById('txtInput').value=obj.name ;
}
function SetValue()
{
    var obj = new Object();
    obj.name =  document.getElementById('txtInput').value;
    window.returnValue = obj;
    window.close();
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" />
</body>

 

抱歉!评论已关闭.