Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
Although Internet Explorer provides an excellent alternative to displaying script output in a command window, you can encounter a problem if the browser window is closed before your script finishes running. For example, you might have a script that creates an instance of Internet Explorer and then writes service information to the document window. If Internet Explorer is closed before you finish writing the service information, the script will fail; this occurs because the script is attempting to write to an instance of the browser that no longer exists.
To avoid this problem, you can configure your script to respond to Internet Explorer events. When Internet Explorer is closed, the browser triggers an onQuit event. Your script can watch for this event and take the appropriate action when it occurs. For example, the script might create a new instance of Internet Explorer, or it might use the Wscript.Quit method to terminate itself.
When creating this instance of Internet Explorer, you must use the Wscript CreateObject method; this is the only way to add event handling to a script. To enable event handling, CreateObject requires two parameters: the ProgID "InternetExplorer.Application" and a second argument consisting of a name to be given to the event handler. This name typically bears some relation to the created object followed by an underscore. For example, this code assigns the name IE_ to the entity responsible for handling Internet Explorer events:
Set objExplorer = WScript.CreateObject(
"InternetExplorer.Application
",
"IE_
")
Although you do not have to follow this naming convention, it does make it easier for someone to read your script and understand how it works.
The event handler then watches for events triggered by Internet Explorer. When an event occurs, the event handler checks the script to see whether any procedures should run in response to that event. For example, this procedure causes the script to terminate itself any time the onQuit event is detected:
Sub IE_onQuit() Wscript.Quit End Sub
In other words, any time the browser window is closed (thus triggering the onQuit event), the script will terminate itself. The script will thus automatically quit any time Internet Explorer quits.
You can create other procedures to respond to other events as well. For example, to respond to the onLoad event fired by Internet Explorer, create a procedure named IE_onLoad().
Note
Listing 17.8 contains a script that terminates itself when the browser window closes. To carry out this task, the script must perform the following steps:
This query returns a collection consisting of all the services installed on the computer.
Because this is a demonstration script, a two-second pause (Wscript.Sleep 2000) is inserted after each service name is written to the browser. This ensures that the script will continue running long enough for you to close the browser window.
To demonstrate how the script responds to Internet Explorer events, start the script and then close the browser window before all the service names have been written. When the browser window closes, the script will automatically terminate itself without generating an error.
Listing 17.8 Stopping a Script When Internet Explorer Closes
|
|
Send us your feedback | « Previous | Next » |