Global Variables Between Parent and Child Documents (Windows)

P

Patient Guy

I have a library of functions representing a filesystem interface
(essentially a file selection interface, to be used in
opening/reading/writing/closing files).

Heavily scripted HTML document #1, very application-like, must include the
JS file for this library of functions. One reason is that it must call a
function to name a "callback" function, and within its own script block,
must define the callback function. The callback handles the selected file
name.

HTML document #2 is essentially a new document (window) created to present
the filesystem/file selection interface as dynamically updated HTML-
formatted tables of the list of file names/properties and subdirectory
names/properties of the current directory.

The problem is that the JS library file includes global variables which
are arrays to store the current directory file and subdirectory info. The
JS file is included through a <SCRIPT> element (as external JS file) in
both HTML documents #1 and #2. Document #2 is created through an open()
window call in Document #1.

The odd thing is: Document #2 creates the arrays in the defined global
variables, and then the array variables are accessed to build the HTML-
formatted tables during the initial creation of Document #2.

But when the user presses a button inside Document #2 to select a file or
folder in a table cell, the arrays are "undefined" ("void").

I am seeing this as a some sort of conflict of global variables between
documents #1 and #2. That is global variables in Document #1 (the parent
window/document) could be used to construct document #2, and then when
document #2 was intially constructed, its inherited global variables were
initialized (undefined globals are "voided").

I have been trying to find something in the JS/ECMA specification which
explains how this works and how to deal with this.
 
L

lallous

Hello

AFAIK, everytime you include a script into a document, new instances of
these variables will be present for that document.
So if you include two JS scripts into two different documents, then each
will have his own copy of global variables.

One way to access global variables between window and opener is to
centralize the location of the global variables and access them
appropriately.

Example:

doc1:
var v1 = 1, v2 = 2;

var w = window.open('doc2.htm', 'doc2');

doc2:

To access v1 and v2 global variables, you have to reach doc1's window
appropriately, and in that case, from doc2:

alert(window.opener.window.v1);


hope that helps,
Elias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top