如何在HTML页面中检测调用它的VBS脚本是否已关闭?
Got it, let's tackle this problem. So you've got a VBScript that launches IE and listens for its close event, but now you need the JS in your my.html to detect when the VBScript itself has ended (since IE keeps running after VBS closes). Here are two reliable approaches to make this work:
Approach 1: Heartbeat Signal (No ActiveX Required)
This method uses a periodic "heartbeat" from the VBScript to the IE window. If the JS stops receiving the heartbeat, it knows the VBS has terminated.
Step 1: Update your VBScript
Modify your VBS to send a regular signal to the IE window's global scope, while keeping the script running to maintain the heartbeat:
Set oIE_ = WScript.CreateObject("InternetExplorer.Application", "IE_") oIE_.Visible = True oIE_.navigate "my.html" ' Wait for IE to finish loading Do While oIE_.Busy Or oIE_.ReadyState <> 4 WScript.Sleep 100 Loop ' Send heartbeat to IE every second Do ' Set a global variable in IE's window to signal VBS is alive oIE_.Document.parentWindow.VBSAlive = True WScript.Sleep 1000 Loop Until False Sub IE_onQuit MsgBox "IE closed !" WScript.Quit ' Exit VBS when IE closes End Sub
Step 2: Add Detection Logic to your JavaScript
In my.html, add a timer to check for the heartbeat. If it stops for longer than the interval, trigger your termination handling:
let lastHeartbeatTime = Date.now(); const heartbeatCheck = setInterval(() => { if (typeof window.VBSAlive !== 'undefined' && window.VBSAlive) { lastHeartbeatTime = Date.now(); window.VBSAlive = false; // Reset for next heartbeat } else { // If no heartbeat for 2 seconds, assume VBS has ended if (Date.now() - lastHeartbeatTime > 2000) { clearInterval(heartbeatCheck); alert("VBS process has ended!"); // Add your custom logic here (e.g., prompt user, close IE, etc.) } } }, 500);
Pros: Works without requiring IE to allow ActiveX controls, so better for restricted environments.
Cons: Relies on the VBScript running its loop—if the VBS is force-killed, the heartbeat stops immediately (which is exactly what we want, so this isn't a big issue).
Approach 2: Process ID Detection (Direct Process Check)
This method uses Windows WMI via ActiveX to check if the VBScript's process is still running. It requires IE to allow ActiveX, but it's a more direct check.
Step 1: Pass VBScript PID to IE
Modify your VBS to get its own process ID and pass it to my.html via the URL:
' Get current VBS process ID Set oShell = CreateObject("WScript.Shell") Set oProc = oShell.Exec("cmd.exe /c echo %PID%") vbsPID = oProc.StdOut.ReadLine() Set oIE_ = WScript.CreateObject("InternetExplorer.Application", "IE_") oIE_.Visible = True ' Pass PID as a URL parameter to my.html oIE_.navigate "my.html?vbsPID=" & vbsPID Do While oIE_.Busy Or oIE_.ReadyState <> 4 WScript.Sleep 100 Loop Sub IE_onQuit MsgBox "IE closed !" WScript.Quit End Sub ' Keep VBS running until IE closes Do While True WScript.Sleep 1000 Loop
Step 2: Check for VBS Process in JavaScript
In my.html, parse the PID from the URL and use WMI to check if the process exists:
// Extract VBS PID from URL parameters const urlParams = new URLSearchParams(window.location.search); const vbsPID = urlParams.get('vbsPID'); if (vbsPID) { const processCheck = setInterval(() => { try { // Use WMI to check if the VBS process is still running const wmiLocator = new ActiveXObject("WbemScripting.SWbemLocator"); const wmiService = wmiLocator.ConnectServer(".", "root/cimv2"); // This will throw an error if the process doesn't exist wmiService.Get(`Win32_Process.Handle='${vbsPID}'`); } catch (error) { // Process not found—VBS has ended clearInterval(processCheck); alert("VBS process has ended!"); // Add your custom logic here } }, 1000); }
Pros: Directly checks the OS process, so it's reliable even if the VBS crashes unexpectedly.
Cons: Requires IE's security settings to allow ActiveX controls (which may be disabled in locked-down environments).
Quick Note on IE Persisting After VBS Closes
IE runs as a separate process from your VBScript, so closing the VBS won't automatically terminate IE. Both methods above let your JS detect when the VBS is gone, and you can add code to close IE if needed (just call window.close() in JS once you detect termination).
内容的提问来源于stack exchange,提问作者Teddy

