WSH » WScript » CreateObject

Syntax:
WScript.CreateObject (strProgID, [strSubPrefix])
strProgID
Receives the ID that will be assigned to the newly-created object.
strSubPrefix
If non-empty, the CreateObject method will bind the events of the newly created object to event handlers beginning with the string passed to this parameter.

The CreateObject method creates an instance of a COM component with a specified ProgID and, optionally, registers the current script as a handler for events generated by the newly created object.

If the strSubPrefix argument is non-empty, then this method will bind the events of the newly created object to event handlers (sinks) in the currently executing script. After such a call to CreateObject, whenever an event in the newly created object occurs, WSH will look for a subroutine in the currently executing script whose name is the concatenation of strSubPrefix and the name of the event being fired. If this subroutine has the correct prototype to act as an event-handler for the event, then WSH will call it and pass it the appropriate arguments. Note that WSH looks for a subroutine whose name is the direct concatenation of the specified strSubPrefix and the event name - no underscore is included in the name unless it is specified in strSubPrefix.

Examples

Code:
Set objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = True
boolBrowserRunning = True
 
Do While objIE.Visible
    WScript.Sleep 500
Loop

Sub objIE_NavigateComplete2(ByVal pDisp, URL)
    WScript.Echo "You just navigated to", URL
End Sub

Sub objIE_OnQuit()
    boolBrowserRunning = False
End Sub
Output:
You just navigated to http://www.devguru.com
You just navigated to http://www.devguru.com/top.html
You just navigated to http://www.devguru.com/frameset.asp
You just navigated to http://www.devguru.com/menu.asp
You just navigated to http://www.devguru.com/home.asp
Explanation:

This VBScript code creates an instance of Internet Explorer and establishes an event handler for its NavigateComplete2 event. When IE is opened, if the user navigates to http://www.devguru.com and then closes the browser, the output shown will be generated.

Language(s): VBScript
Code:
objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = true

while (objIE.Visible)
{
    WScript.Sleep(500)
}

function objIE_NavigateComplete2(pDisp, URL)
{
    WScript.Echo("You just navigated to", URL)
}

function objIE_OnQuit()
{
    boolBrowserRunning = false ;
}
Output:
You just navigated to http://www.devguru.com
You just navigated to http://www.devguru.com/top.html
You just navigated to http://www.devguru.com/frameset.asp
You just navigated to http://www.devguru.com/menu.asp
You just navigated to http://www.devguru.com/home.asp.
Explanation:

This JScript code creates an instance of Internet Explorer and establishes an event handler for its NavigateComplete2 event. When IE is opened, if the user navigates to http://www.devguru.com and then closes the browser, the output shown will be generated.

Language(s): JavaScript

See Also: