VBScript readyState Property
last modified April 9, 2025
The readyState
property in VBScript is used with XMLHTTP objects to
track the state of HTTP requests. It returns a numeric value indicating the
current state of the request. This property is essential for asynchronous
operations where you need to monitor request progress.
readyState
has five possible values from 0 to 4, each representing
a different stage. It's commonly used with the onreadystatechange
event handler. This tutorial covers readyState
with practical
examples to demonstrate its usage.
readyState Property Overview
The readyState
property indicates the current state of an XMLHTTP
request. The values range from 0 (UNSENT) to 4 (DONE). Each value represents a
specific stage in the request lifecycle. Understanding these states is crucial
for proper request handling.
State 0 means the request isn't initialized. State 1 means the connection is established. State 2 means the request is received. State 3 means processing. State 4 means the operation is complete. These states help track asynchronous request progress.
Basic readyState Monitoring
This example demonstrates basic monitoring of the readyState
property. It shows how to check the state during an HTTP request. The script
makes a simple GET request and logs state changes.
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") xmlhttp.Open "GET", "https://example.com", False xmlhttp.Send WScript.Echo "ReadyState: " & xmlhttp.readyState WScript.Echo "Status: " & xmlhttp.status Set xmlhttp = Nothing
The script creates an XMLHTTP object and makes a synchronous request. The
readyState
is checked after sending the request. For synchronous
requests, the state will be 4 (complete) when execution continues.
Asynchronous Request with State Tracking
This example shows how to track readyState
changes in an
asynchronous request. It uses the onreadystatechange
event to
monitor progress. Each state change triggers the event handler.
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") Sub OnReadyStateChange WScript.Echo "State changed to: " & xmlhttp.readyState If xmlhttp.readyState = 4 Then WScript.Echo "Request completed with status: " & xmlhttp.status End If End Sub xmlhttp.onreadystatechange = GetRef("OnReadyStateChange") xmlhttp.Open "GET", "https://example.com", True xmlhttp.Send ' Keep script running while request completes Do While xmlhttp.readyState <> 4 WScript.Sleep 100 Loop Set xmlhttp = Nothing
The script creates an asynchronous request and defines an event handler. The handler logs each state change. The loop ensures the script waits for completion. This pattern is common for asynchronous operations.
Handling Different readyState Values
This example demonstrates handling all possible readyState
values.
It shows how to perform different actions based on the current state. Each state
triggers specific processing.
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") Sub StateHandler Select Case xmlhttp.readyState Case 0: WScript.Echo "Request not initialized" Case 1: WScript.Echo "Server connection established" Case 2: WScript.Echo "Request received" Case 3: WScript.Echo "Processing request" Case 4: WScript.Echo "Request finished" If xmlhttp.status = 200 Then WScript.Echo "Response: " & xmlhttp.responseText End If End Select End Sub xmlhttp.onreadystatechange = GetRef("StateHandler") xmlhttp.Open "GET", "https://example.com", True xmlhttp.Send Do While xmlhttp.readyState <> 4 WScript.Sleep 100 Loop Set xmlhttp = Nothing
The script uses a Select Case
structure to handle each state. State
4 includes additional status checking. This approach provides fine-grained
control over request processing. It's useful for complex operations.
Error Handling with readyState
This example demonstrates error handling using readyState
and
status codes. It shows how to detect and respond to request failures. Proper
error handling makes scripts more robust.
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") Sub ErrorHandler If xmlhttp.readyState = 4 Then If xmlhttp.status = 200 Then WScript.Echo "Success: " & xmlhttp.responseText Else WScript.Echo "Error: " & xmlhttp.status & " - " & xmlhttp.statusText End If End If End Sub xmlhttp.onreadystatechange = GetRef("ErrorHandler") xmlhttp.Open "GET", "https://example.com/nonexistent", True xmlhttp.Send Do While xmlhttp.readyState <> 4 WScript.Sleep 100 Loop Set xmlhttp = Nothing
The script checks both readyState
and status
for error
handling. Only when the request is complete (state 4) does it evaluate the
status. This pattern ensures proper error detection after request completion.
File Download with Progress Tracking
This example shows how to use readyState
to track file download
progress. It demonstrates intermediate processing during state 3. The script
downloads a file while reporting progress.
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") Sub ProgressHandler If xmlhttp.readyState = 3 Then WScript.Echo "Download in progress..." ElseIf xmlhttp.readyState = 4 Then If xmlhttp.status = 200 Then Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.CreateTextFile("downloaded_file.html", True) file.Write xmlhttp.responseText file.Close WScript.Echo "File downloaded successfully" End If End If End Sub xmlhttp.onreadystatechange = GetRef("ProgressHandler") xmlhttp.Open "GET", "https://example.com", True xmlhttp.Send Do While xmlhttp.readyState <> 4 WScript.Sleep 100 Loop Set xmlhttp = Nothing
The script uses state 3 to indicate download progress. Upon completion (state 4), it saves the response to a file. This demonstrates practical use of intermediate states. The approach works well for large file downloads.
Source
In this article, we have explored the readyState
property in
VBScript, covering its usage and practical applications. From basic state
monitoring to complex download tracking, these examples demonstrate request
state management. With this knowledge, you can enhance your HTTP operations with
robust state handling.
Author
List all VBScript tutorials.