Javascript 两个窗体之间传值实现代码

Javascript两个窗体之间传值实现代码

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:

复制代码代码如下:

<scripttype="text/javascript">

functionOpenChildWindow()

{

window.showModalDialog('ChildPage.htm',document.getElementById('txtInput').value);

}

</script>

<inputtype="text"id="txtInput"/>

<inputtype="button"value="OpenChild"onclick="OpenChildWindow()"/>

ChildPage.htm:

复制代码代码如下:

<bodyonload="Load()">

<scripttype="text/javascript">

functionLoad()

{

document.getElementById('txtInput').value=window.dialogArguments;

}

</script>

<inputtype="text"id="txtInput"/>

</body>

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

XML-Code:

复制代码代码如下:

<scripttype="text/javascript">

functionOpenChildWindow()

{

varargs=newArray();

args[0]=document.getElementById('txtInput').value;

window.showModalDialog('ChildPage.htm',args);

}

</script>

<inputtype="text"id="txtInput"/>

<inputtype="button"value="OpenChild"onclick="OpenChildWindow()"/>ChildPage.htm:

XML-Code:

<scripttype="text/javascript">

functionLoad()

{

document.getElementById('txtInput').value=window.dialogArguments[0];

}

</script>

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

XML-Code:

复制代码代码如下:

<scripttype="text/javascript">

functionOpenChildWindow()

{

varobj=newObject();

obj.name=document.getElementById('txtInput').value;

window.showModalDialog('ChildPage.htm',obj);

}

</script>

<inputtype="text"id="txtInput"/>

<inputtype="button"value="OpenChild"onclick="OpenChildWindow()"/>

ChildPage.html:

XML-Code:

复制代码代码如下:

<scripttype="text/javascript">

functionLoad()

{

varobj=window.dialogArguments;

document.getElementById('txtInput').value=obj.name;

}

</script>

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

XML-Code:

复制代码代码如下:

<scripttype="text/javascript">

functionOpenChildWindow()

{

varobj=newObject();

obj.name=document.getElementById('txtInput').value;

varresult=window.showModalDialog('ChildPage.htm',obj);

document.getElementById('txtInput').value=result.name;

}

</script>

<inputtype="text"id="txtInput"/>

<inputtype="button"value="OpenChild"onclick="OpenChildWindow()"/>

ChildPage.html:

XML-Code:

复制代码代码如下:

<bodyonload="Load()">

<scripttype="text/javascript">

functionLoad()

{

varobj=window.dialogArguments;

document.getElementById('txtInput').value=obj.name;

}

functionSetValue()

{

varobj=newObject();

obj.name=document.getElementById('txtInput').value;

window.returnValue=obj;

window.close();

}

</script>

<inputtype="text"id="txtInput"/>

<inputtype="button"value="SetFather"onclick="SetValue()"/>

</body>

详细出处参考:http://www.jb51.net/article/20141.htm

相关推荐