开发中经常遇到这样的问题:需要在一个页面里打开另外的页面(模态,或者非模态)。
见过很多这样的例子,另外还有页面间需要传递参数的,大部分都是采用地址栏参数拼接的形式。这很显然是一个不好的编程习惯,每次需要增加参数的时候都要面对那段可恶的参数拼接,经常出错。特别是最后一个参数如果是空串的话,容易产生下标越界的错误(因为split后,数组根本不会动态分配存储空间),所以一般需要增加额外的保护变量(一个与参数无关的变量),保证数组的内容如自己想的一样。
这个时候,窗口自带的属性dialogArguments还是比较强大的,代码上有些改观,但是拆数据的时候还是得很小心,否则还是会出错的:
找来的一段代码,或许能很好的说明这一点:
Retrieves the variable or array of variables passed into the modal dialog window.
dhtml语法
[ vVariables = ] window.dialogArguments
DHTML可能的值
vVariables String, numeric, object, or array value that specifies arguments. The property is read-only. The property has no default value.
Remarks
The dialogArguments property applies only to windows created using the showmodaldialog and showmodelessdialog methods.
Examples
The following example shows how to retrieve information passed into a modal dialog window using the dialogArguments property. This example consists of two snippets of code, which correspond to two different files. One file launches the modal window and the other file stores the code for the modal window.
This file launches the modal window and sends an object to that modal window.
<HTML> <HEAD> <SCRIPT> function fnLaunch() { var aForm; aForm = oForm.elements; var myObject = new Object(); myObject.firstName = aForm.oFirstName.value; myObject.lastName = aForm.oLastName.value; // The object "myObject" is sent to the modal window. window.showModalDialog("modalDialogSource.htm", myObject, "dialogHeight:300px; dialogLeft:200px;"); } </SCRIPT> </HEAD> <BODY> <BUTTON onclick="fnLaunch();" >Launch The Window</BUTTON> <FORM ID= "oForm"> First Name: <INPUT TYPE="text" NAME="oFirstName" VALUE="Jane"> <BR> Last Name: <INPUT TYPE="text" NAME="oLastName" VALUE="Smith"> </FORM> </BODY> </HTML>This file (modalDialogSource.htm), stores the code for the modal window. The object sent to this modal window is retrieved using the dialogArguments property.
<HTML> <HEAD> <SCRIPT> var oMyObject = window.dialogArguments; var sFirstName = oMyObject.firstName; var sLastName = oMyObject.lastName; </SCRIPT> <title>Untitled</title> </head> <BODY STYLE="font-family: arial; font-size: 14pt; color: Snow; background-color: RosyBrown;"> First Name: <SPAN STYLE="color:00ff7f"> <SCRIPT> document.write(sFirstName); </SCRIPT> </SPAN> <BR> Last Name: <SPAN STYLE="color:00ff7f"> <SCRIPT> document.write(sLastName); </SCRIPT> </SPAN> </BODY> </HTML>