WebReference.com - Part 2 of chapter 5 from Creating Applications with Mozilla. From O'Reilly (5/5). | WebReference

WebReference.com - Part 2 of chapter 5 from Creating Applications with Mozilla. From O'Reilly (5/5).

To page 1To page 2To page 3To page 4current page
[previous]

Creating Applications with Mozilla, Chapter 5: Scripting Mozilla

Sharing Data Between Documents

As the scale of your application development increases and your applications grow new windows and components, you may become interested in passing data around and ensuring that the data remains in scope. Misunderstanding that scope often leads to problems when beginning Mozilla applications.

Scope in Mozilla

The general rule is that all scripts pulled in by the base XUL document and scripts included in overlays of this document are in the same scope. Therefore, any global variables you declare in any of these scripts can be used by any other scripts in the same scope. The decision to put a class structure or more sophisticated design in place is up to you.

The relationship of a parent and child window indicates the importance of storing data in language constructs that can be passed around. This code shows a common way for a parent to pass data to a window it spawns:

var obj = new Object ( );
obj.res = "";
window.openDialog("chrome://xfly/content/foo.xul", 'foo_main',
"chrome,resizable,scrollbars,dialog=yes,close,modal=yes",
obj);

Using the window.arguments array

The previous code snippet creates a new JavaScript object, obj, and assigns the value of an empty string to that object's res property. The object is then passed by reference to the new window as the last parameter of the openDialog( ) method so it can be manipulated in the scope of the child window:

function onOk( ) {
window.arguments[0].res  = "ok";
return;
}
function onCancel( ) {
window.arguments[0].res  = "cancel";
return;
}

In that child window, the object is available as an indexed item in the special window.arguments array. This array holds a list of the arguments passed to a window when it is created. window.arguments[0] is a reference to the first argument in the openDialog( ) parameter list that is not a part of the input parameters for that method, window.arguments[1] is the second argument, and so on. Using window.arguments is the most common way to pass objects and other data around between documents.

When the user clicks a button in the displayed dialog (i.e., the OK or Cancel button), one of the functions sets a value to the res property of the passed-in object. The object is in the scope of the newly created window. When control is passed back to the script that launched the window, the return value can be checked:

if (obj.res != "ok") {
dump("User has cancelled the dialog");
return;
}

In this case, a simple dump statement prints the result, but you can also test the result in your application code and fork accordingly.


To page 1To page 2To page 3To page 4current page
[previous]

Created: September 26, 2002
Revised: September 26, 2002

URL: https://webreference.com/programming/javascript/mozillaapps/chap5/2/5.html