| |
Manipulating a Separate Window
For this article I'll be using some code from the Windows
Script Component (WSC) section of this site. When viewing the ShellInfo WSC example, there are links that provide
additional information on Special Folder names and Environment Variables. This information
is displayed in a pop-up window, and this window (if it remains open) is used to fulfill
subsequent requests for additional information.. |
Some basic introductory information:
|
 |
This window can be referenced by using self |
 |
The window object has a location property which is an object
with properties and methods relating to the URL associated with the window
object. |
 |
The search property of the location object specifies the
Query String passed to web applications like CGI scripts or Active Server Pages (ASP)
scripts. |
|
Demonstration
Requirements
- Mange the open/closed state of the window.
- Dynamically specify the windows's document.
- Close the window when it is no longer needed.
To manage the state of the window-that is to determine whether or not it
has been open and remains open-an object variable is used to store a reference to the
opened window. The name property is the key element. It is specefied when the
window is first open, and is set to an empty string when the window is closed (handled in onUnload ),
making it possible to know if the user closed the window. |
var wHelp = new Object();
wHelp.name = "";
...
if ( wHelp.name != "" ) { // window is already open
...
} else { // window is not open
...
}
|
The document is specified when the window is first open, as the first
argument passed to self.open() . |
self.open(url, name, features);
|
In this application the same URL is maintained. The document is generated
by a CGI script, and its contents depend on the value passed in the Query String. All that
needs changing is the Query String. This is done by setting the search
property of the location property of the window object (window.location.search ).
When the search property is changed a request is made to the server with the
new Query String. |
wHelp.location.search = "?item=" + s;
|
Closing the window is accomplished with the window object's close()
method. When the document in the parent window is unloaded a confirmation dialog box is
displayed asking the user if the child window should be closed, and conditionally self.close()
is called. |
if ( wHelp.name != "" ) {
if ( confirm("Would you like to close the help window?") ) {
wHelp.close();
}
}
|
The openHelp(s) function below is where all the work is done
to open the window or change the document in it.. It accepts one String parameter that
specifies the value to be used in the Query String passed to the CGI script where it is
used to determine the content to display; details for either Environment Variables or
Special Folder names. |
There are different steps to take depending on whether or not the window is open.
If it is open, then:
- Change the
search property of the location property of the window
object. to the needed Query String. This will make another request for the document with
the new Query String.
- Move the window into the foreground by calling the
focus() method.
If it is not open:
- Build a string specifying the features for the new window.
- Delete the old object variable (anything allocated using
new should be
deallocated using delete ).
- Open the new window and store a reference to it.
- Move the window to the foreground by calling
focus()
Note: The try {} catch () {} exception handling blocks are
features only available in Microsoft's implementation of JavaScript known as JScript used
by Internet Explorer. If you want to make this code platform-independent then remove the
exception handling blocks. |
function openHelp( s ) {
try {
if (wHelp.name != "") { // reload the help script in the open window and focus it.
wHelp.document.location.search = "?item=" + s;
wHelp.focus();
} else {
var sFeatures = "toolbar=no,location=no,menubar=no,scrollbars=yes,resizeable=yes,status=yes,width=";
sFeatures += parseInt( (screen.availWidth * 0.75) + "", 10 );
sFeatures += ",height=" + parseInt( (screen.availHeight* 0.75) + "", 10 );
delete wHelp;
wHelp = self.open("../cgi-bin/wsc_help.pl?item=" + s, "WSCHELP", sFeatures);
wHelp.focus();
}
} catch (e) {
alert("Error opening help window: " + e);
}
}
function closeHelp() {
if ( wHelp.name != "" ) {
if ( confirm("do you want to close the help window?") ) {
wHelp.close();
}
}
}
|