Stopping a Script When Internet Explorer Is Closed

Microsoft® Windows® 2000 Scripting Guide

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_")
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()
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

  • Event handling will not work with all scripts. WSH scripts automatically terminate themselves when the last line of code is run; in some cases, therefore, the script will terminate itself before the Internet Explorer event can be fired. If this is a problem, you might have to use Wscript.Sleep to pause the script for a specified amount of time, or you might have to create a loop that keeps the script running until a particular event is detected.

Scripting Steps

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:

  1. Use the Wscript CreateObject method to create an instance of Internet Explorer, and assign the name IE_ to the event handler responsible for monitoring Internet Explorer events.

  2. Open a blank Web page by navigating to "about:blank".

  3. Configure various Internet Explorer properties, such as the width and height of the window, and hide items such as the toolbar and status bar.

  4. Set the Visible property to 1 to display the instance on the screen.

  5. Create an object reference to the Document object.

  6. Prepare the Web page for writing by opening the Document object (objDocument.Open).

  7. Create a variable to specify the computer name.

  8. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  9. Use the ExecQuery method to query the Win32_Service class.

    This query returns a collection consisting of all the services installed on the computer.

  10. For each service in the collection, use the WriteLn method to write the service display name and a carriage return/linefeed (<BR>) to the browser window.

    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.

  11. Create a subroutine named IE_onQuit(). This subroutine will be called whenever Internet Explorer is closed.

  12. If the subroutine is called (meaning Internet Explorer has been closed), use the Wscript.Quit method to terminate the script.

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

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Set objExplorer = WScript.CreateObject _
 ("InternetExplorer.Application", "IE_")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 250
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Visible = 1
Set objDocument = objExplorer.Document
objDocument.Open
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
Set colServices = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Service")
For Each objService in colServices
 objDocument.Writeln objService.DisplayName & "<BR>"
 Wscript.Sleep 2000
Next
Sub IE_onQuit()
 Wscript.Quit
End Sub