August 29, 2001 - Modifying a Global Variable from within the Dialog Box | WebReference

August 29, 2001 - Modifying a Global Variable from within the Dialog Box

Yehuda Shiran August 29, 2001
Modifying a Global Variable from within the Dialog Box
Tips: August 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

When you call an Internet Explorer dialog box with either showModalDialog() or showModelessDialog(), you can use the second parameter to pass information from the caller to the callee and vice versa. Suppose you have a global variable, sColor, in the caller. You want to update the sColor value from within the callee. You need to pass it by reference, but JavaScript passes scalar variables only by value. You know that all global variables are properties of the window object. You can pass the window object to the callee, modify the sColor property, and get the global variable sColor updated this way.

Let's see how we do it in code. The global variable sColor is defined in the caller as:

var sColor=""; 
You call the dialog box as follows:

showModelessDialog("010828b.html",window,
   "status:false;dialogWidth:300px;dialogHeight:150px");
Notice the window object in the second position. The callee reads in the second parameter into the dialogArguments variable. It can be a scalar, an array, an object, etc. In the following example the callee first assigns dialogArguments to the callerWindowObj, and then sets the sColor property of this object to the desired value (oEnterColor.value from the form):

  var callerWindowObj = dialogArguments;
  callerWindowObj.sColor = oEnterColor.value;
For more on modal and modeless dialog boxes, go to Column 90, Modal and Modeless Dialog Boxes.