472,111 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

Global Variables Between Parent and Child Documents (Windows)

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.

Sep 16 '05 #1
2 8988
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

"Patient Guy" <Pa*********@nowhere.to.be.found.com> wrote in message
news:Xn******************@207.115.17.102...
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.

Sep 16 '05 #2

"lallous" <la*****@lgwm.org> wrote in message
news:3o************@individual.net...
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


this is exactly what I was going to say.

Include all the global vars in the parent scripts and have the child page
reference them there.
Sep 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rustam Asgarov | last post: by
3 posts views Thread by Maheshkumar.R | last post: by
2 posts views Thread by Chuck | last post: by
1 post views Thread by Raed Sawalha | last post: by
3 posts views Thread by jimmygoogle | last post: by
10 posts views Thread by Goran Djuranovic | last post: by
reply views Thread by Iain | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.