Modal and Modeless Dialog Boxes: Passing Parameters by Value and by Reference - Doc JavaScript | WebReference

Modal and Modeless Dialog Boxes: Passing Parameters by Value and by Reference - Doc JavaScript


Modal and Modeless Dialog Boxes

Passing Parameters by Value and by Reference

The second argument of the functions showModalDialog() and showModelessDialog() can be of any type: scalar, array, string, object, etc. This argument can be used to pass parameters by value from the caller to the callee. Let's take a look at a simple example. Pop up a dialog box with this statement:

window.showModelessDialog('7a.html','Doc JavaScript');

The page 7a.html includes some text and the following script:

<SCRIPT LANGUAGE="JavaScript">
<!--
  alert(dialogArguments);
// -->
</SCRIPT>

The window object property dialogArguments is set to the second parameter passed by showModelessDialog(). We passed the string "Doc JavaScript", and indeed the alert box in 7a.html pops up this string. Try displaying 7a.html by itself, and you'll get an undefined parameter error (dialogArguments).

Above, we passed the parameter by value. 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 first. 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('7b.html',a);

The callee 7b.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 7b.html to change it:

window.showModelessDialog('7b.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".

Next: How to pass objects to dialog boxes

http://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: August 13, 2001
Revised: August 13, 2001

URL: http://www.webreference.com/js/column90/7.html