philipthegreat
philipthegreat

Reputation: 244

Editing HTML with VBScript in an HTA

I have an HTA (html application) for adding network printers. It consists of some check boxes with printer names, and a button that initiates the install.

Befuddling to me is that the output_area.innerHTML call within the If statement does nothing (though the printer does install properly), while the output_area.innerHTML call outside of the If statement works just fine.

<script language="VBScript">
 Sub AddPrinterSub
    Set Network = CreateObject("WScript.Network")
    If printer-name.Checked Then
       output_area.innerHTML = "Installing printer-name"
       Network.AddWindowsPrinterConnection "\\server-name\printer-name"
    End If
    output_area.innerHTML = "Printer install complete."
 end sub
</script>

<body>
 <input type="checkbox" name="printer-name">Printer Name
 <input type="button" value="Add Printer" onClick="AddPrinterSub">
 <div id="output_area"></div>
</body>

While the printer is installing, the output_area div shows nothing; after the install is complete, it shows "Printer install complete."

Upvotes: 2

Views: 4993

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

While your Sub is executed, the browser does not update the display. You can use

<html>
 <head>
  <script type="text/vbscript">
    Option Explicit
    Sub AddPrinterSub
       Dim i
       For i = 1 To 5
          output_area.innerHTML = "Installing printer " & i
          CreateObject("WScript.Shell").Run "ping -n 5 127.0.0.1", 0, True
       Next
       output_area.innerHTML = "Printer install complete."
    end sub
  </script>
 </head>
 <body>
  <input type="checkbox" name="printer-name">Printer Name
  <input type="button" value="Add Printer" onClick="AddPrinterSub">
  <div id="output_area"></div>
 </body>
</html>

to prove that the update 'works' as soon as you put (the .HTA version of) .Sleep into your loop.

You must decide whether the detailed display is worth the extra work and - if yes - whether shelling out or using setTimeout for the installation is more appropriate.

Upvotes: 0

Tmdean
Tmdean

Reputation: 9299

This is kind of hard to explain, but when you change the DOM in a script, the DOM doesn't actually change until the script finishes executing. You never see "Installing printer-name" because the div is only set to this text while the script is executing. If you want to see the status message, you need to break the script into two parts: one to initialize the long-running task, and the other to actually execute the task.

In order to do this, you can use the function SetTimeout, which sets an event to execute a function after a delay. In this case, we don't need a delay - we only care that SetTimeout lets us start executing a function after the current script finishes executing, giving the DOM a chance to refresh, so we use a delay of 0.

Sub AddPrinterSub()
    If printer_name.Checked Then
        SetTimeout "AddPrinter ""\\server-name\printer-name""", 0
        output_area.innerHTML = "Installing printer-name"
    End If
end sub

Sub AddPrinter(printer_path)
    Set Network = CreateObject("WScript.Network")
    Network.AddWindowsPrinterConnection printer_path
    output_area.innerHTML = "Printer install complete."
End Sub

Upvotes: 3

Related Questions