August 27, 2001 - Passing an Array to the Dialog Box | WebReference

August 27, 2001 - Passing an Array to the Dialog Box

Yehuda Shiran August 27, 2001
Passing an Array to the Dialog Box
Tips: August 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

When passing a parameter by value to the dialog box, the callee can only read the passed parameter, but cannot change it in the caller page. In order to be able to change the caller, you need to pass the parameter by reference. You do this when you pass the address of a variable. There are at least two ways to pass addresses: an array and an object. Let's demonstrate the array passing. In this page (the caller), we define an array a as follows:

<SCRIPT LANGUAGE="JavaScript">
<!--
  var a = new Array;
  a[0]="first";
  a[1]="second";
  a[2]="third";
// -->
</SCRIPT>

And we pass the array a to the dialog box:

window.showModelessDialog('010827a.html',a);
The callee 010827a.html includes the following script:

<SCRIPT LANGUAGE="JavaScript">
<!--
  a = dialogArguments;
  a[0] = "fourth";
// -->
</SCRIPT>

The callee changed the first element of the array and it should be reflected in the caller page. Let's try it. First, let's make sure the array a is as we initialized it: "first,second,third". Now, call 010827a.html to change it:

window.showModelessDialog('010827a.html',a);
Notice how we pass the array a as the second argument of showModelessDialog(). Feel free to close the dialog box. The dialog box already modified a[0], so you should see a modified array: "fourth,second,third".

For more on modal and modeless dialog boxes, go to Column 90, Modal and Modeless Dialog Boxes.