none
HTA windows RRS feed

  • Question

  • How can I open new window from HTA, with same security settings as 'The Opener' one.
    Wednesday, July 22, 2009 11:52 AM

Answers

  • Yes, the modal and modeless windows can be written from the creating HTA.  Here is an example I wrote a while back that does just that.  It opens a window and causes it to blink as a progress indicator.

    It's a bit long and fairly involved, but illustrates a lot of what can be done with this approach ...

    <html>
    <head>
      <TITLE>Modeless Window Test</TITLE>
      <HTA:APPLICATION ID="oApp"
        APPLICATIONNAME="ModelessWdw"
        SHOWINTASKBAR="yes"
        SINGLEINSTANCE="yes"
    >
    </head>

    <SCRIPT Language = "VBScript">
    Dim g_sGrnHTML, g_MsgArea ' global variables

    Sub RunIt
      DataArea.InnerHTML = "<font face='Arial' size='4'><b>" _
                         & "Running</b></font>"
      nLeft = screenleft : nTop  = screentop
    ' Create modeless window
      set g_MsgArea = window.showModelessDialog("about:blank", null, _
                      "dialogLeft:" & nLeft & "px;dialogTop:" & ntop _
                    & "px;dialogHeight:100px;dialogWidth:400px;" _
                    & "center:no;scroll:no;status:no;resizable:no")
    ' Write document to modeless window
      with g_MsgArea.document
        .open
        .writeln "<html><head><title>Working - patience please"
        .writeln "</title></head><SCRIPT Language='VBScript'>"
        .writeln "Dim g_bBlank, g_sRed, g_interval"
        .writeln "sub startit"
        .writeln "g_sRed=""<br><font color=red face=Arial size=4>" _
               & "<center><b>Processing ...</b></center></font>"""
    ' Delay to allow modeless window page to repaint
        .writeln "g_interval = setInterval(""Blink"", 250, ""vbscript"")"
    '
        .writeln "end sub"
        .writeln "Sub Blink"
        .writeln "  if g_bBlank then"
        .writeln "    MsgArea.InnerHTML=g_sRed"
        .writeln "  else"
        .writeln "    MsgArea.InnerHTML="""""
        .writeln "  end if"
        .writeln "  g_bBlank=not g_bBlank"
        .writeln "end sub"
    ' *DO NOT* remove the 'unnecessary' concatenation from next line
    ' of code.  It is needed for correct parsing & execution
        .writeln "</" & "script><body onload='startit'>"
        .writeln "<span id=MsgArea></span></body></html>"
        .close
        Do Until .ReadyState = "complete" : Loop
      End with

      g_sGrnHTML = "<font color='green' face='Arial' size='4'><b>" & _
                 "Completed!! Total Count: nTotals</b></font>"
    ' Allow page to repaint
      setTimeout "CountEvents", 50, "vbscript"
    end sub

    Sub CountEvents ' an example routine - doesn't do anything important
    Dim n, i
      Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
      Set cEvents = oWMI.ExecQuery("Select * From Win32_NTLogEvent")
    '
    ' This loop is just here to slow things down as an example of a
    ' long running process
    '
      for i = 1 to 100*cEvents.count
        n = n + 1
      next
    '
      DataArea.InnerHTML = Replace(g_sGrnHTML, "nTotals", cEvents.count)

    ' Clear window timer and display completed message
      with g_MsgArea.document
        locInterval = .parentWindow.g_interval
        .parentWindow.clearInterval locinterval
        .all.MsgArea.InnerHTML = "<br><font color=lightgreen face=Arial size=4>" _
                               & "<center><b>Complete</b></center></font>"
      end with

    ' Wait one second before closing progress window
      setTimeout "g_MsgArea.close", 1000, "vbscript"
    end sub

    </SCRIPT>
    <body onload=RunIt>
    <input type="button" value=" Do it again " onclick="RunIt">
    <p><hr><p>
    <span id=DataArea></span>
    </body></html>

    Tom Lavedas
    • Marked as answer by CraigLieb Sunday, January 10, 2010 7:51 PM
    Thursday, August 6, 2009 3:08 PM
    Moderator

All replies

  • Hi stnmr,

    Have you tried the WshShell.Run() method?

    Bill
    Wednesday, July 22, 2009 8:23 PM
    Moderator
  • I use WshShell.Run() when need to start external program /Notepad, IE ..../.
    I tried "window.Open" in JavaScript and VBscript, but that opens IE window.
    Probably trough WshShell.Run() can run secondary HTA, but then how to achieve communication between these two HTA-s (variable and parameter exchange).
    If you have something in mind, i'm ready to try it.

    Sorry if my English is bad.
    Thursday, July 23, 2009 6:47 AM
  • Hi stnmr,

    You can use an environment variable, temporary file, or registry value to communicate between HTAs.

    Bill
    Thursday, July 23, 2009 8:58 AM
    Moderator
  • Can you give me example for communication trough environment variables
    Thursday, July 23, 2009 10:05 AM
  • Yes. Here is Master.hta:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <hta:application
      applicationName = "Master.hta"
      version = "1.0"
    />
    <title>Master.hta</title>
    <script language="JavaScript">
    function spawnHTA() {
      var wshShell = new ActiveXObject("WScript.Shell");
      var wshEnv = wshShell.Environment("PROCESS");
      wshEnv.Item("TESTVAR") = "This is TESTVAR's contents";
      wshShell.Run("Sub.hta", 1, true);
    }
    </script>
    </head>
    <body>
    
    <input type="button" value="Spawn HTA" onclick="spawnHTA();" />
    
    </body>
    </html>

    Here is Sub.hta:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <hta:application
      applicationName = "Sub.hta"
      version = "1.0"
    />
    <title>Sub.hta</title>
    <script language="JavaScript">
    function id(elementId) {
      return document.getElementById(elementId);
    }
    
    function updateDiv() {
      var wshEnv = new ActiveXObject("WScript.Shell").Environment("PROCESS");
      id("test").innerHTML = wshEnv.Item("TESTVAR");
    }
    </script>
    </head>
    <body onload="updateDiv();">
    
    <div id="test"></div>
    
    </body>
    </html>

    Bill
    Thursday, July 23, 2009 8:05 PM
    Moderator
  • Thank you Bill. Very useful example.
    If is not too much to ask, will you give me examples for other 2 communication types (Temp file & Registry).

    Thanks again. Stnmr.
    Monday, July 27, 2009 8:52 AM
  • Hi stnmr,

    For the registry: You can use the WshShell object's RegRead and RegWrite methods to create (and remove) a registry value. I recommend creating a temporary subkey in HKCU.

    For a temporary file: You can use the FileSystemObject's GetSpecialFolder() and CreateTempFile() methods to generate a temporary file in the Temp directory, then use the CreateTextFile() method to write to the file.

    Bill
    Monday, July 27, 2009 3:40 PM
    Moderator
  • What about 'Modal' and 'Modeless' Dialog?
    Can they be dynamically fulled with contents, or is absolutely necessary to open existing .HTML file.
    Thursday, August 6, 2009 2:03 PM
  • Yes, the modal and modeless windows can be written from the creating HTA.  Here is an example I wrote a while back that does just that.  It opens a window and causes it to blink as a progress indicator.

    It's a bit long and fairly involved, but illustrates a lot of what can be done with this approach ...

    <html>
    <head>
      <TITLE>Modeless Window Test</TITLE>
      <HTA:APPLICATION ID="oApp"
        APPLICATIONNAME="ModelessWdw"
        SHOWINTASKBAR="yes"
        SINGLEINSTANCE="yes"
    >
    </head>

    <SCRIPT Language = "VBScript">
    Dim g_sGrnHTML, g_MsgArea ' global variables

    Sub RunIt
      DataArea.InnerHTML = "<font face='Arial' size='4'><b>" _
                         & "Running</b></font>"
      nLeft = screenleft : nTop  = screentop
    ' Create modeless window
      set g_MsgArea = window.showModelessDialog("about:blank", null, _
                      "dialogLeft:" & nLeft & "px;dialogTop:" & ntop _
                    & "px;dialogHeight:100px;dialogWidth:400px;" _
                    & "center:no;scroll:no;status:no;resizable:no")
    ' Write document to modeless window
      with g_MsgArea.document
        .open
        .writeln "<html><head><title>Working - patience please"
        .writeln "</title></head><SCRIPT Language='VBScript'>"
        .writeln "Dim g_bBlank, g_sRed, g_interval"
        .writeln "sub startit"
        .writeln "g_sRed=""<br><font color=red face=Arial size=4>" _
               & "<center><b>Processing ...</b></center></font>"""
    ' Delay to allow modeless window page to repaint
        .writeln "g_interval = setInterval(""Blink"", 250, ""vbscript"")"
    '
        .writeln "end sub"
        .writeln "Sub Blink"
        .writeln "  if g_bBlank then"
        .writeln "    MsgArea.InnerHTML=g_sRed"
        .writeln "  else"
        .writeln "    MsgArea.InnerHTML="""""
        .writeln "  end if"
        .writeln "  g_bBlank=not g_bBlank"
        .writeln "end sub"
    ' *DO NOT* remove the 'unnecessary' concatenation from next line
    ' of code.  It is needed for correct parsing & execution
        .writeln "</" & "script><body onload='startit'>"
        .writeln "<span id=MsgArea></span></body></html>"
        .close
        Do Until .ReadyState = "complete" : Loop
      End with

      g_sGrnHTML = "<font color='green' face='Arial' size='4'><b>" & _
                 "Completed!! Total Count: nTotals</b></font>"
    ' Allow page to repaint
      setTimeout "CountEvents", 50, "vbscript"
    end sub

    Sub CountEvents ' an example routine - doesn't do anything important
    Dim n, i
      Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
      Set cEvents = oWMI.ExecQuery("Select * From Win32_NTLogEvent")
    '
    ' This loop is just here to slow things down as an example of a
    ' long running process
    '
      for i = 1 to 100*cEvents.count
        n = n + 1
      next
    '
      DataArea.InnerHTML = Replace(g_sGrnHTML, "nTotals", cEvents.count)

    ' Clear window timer and display completed message
      with g_MsgArea.document
        locInterval = .parentWindow.g_interval
        .parentWindow.clearInterval locinterval
        .all.MsgArea.InnerHTML = "<br><font color=lightgreen face=Arial size=4>" _
                               & "<center><b>Complete</b></center></font>"
      end with

    ' Wait one second before closing progress window
      setTimeout "g_MsgArea.close", 1000, "vbscript"
    end sub

    </SCRIPT>
    <body onload=RunIt>
    <input type="button" value=" Do it again " onclick="RunIt">
    <p><hr><p>
    <span id=DataArea></span>
    </body></html>

    Tom Lavedas
    • Marked as answer by CraigLieb Sunday, January 10, 2010 7:51 PM
    Thursday, August 6, 2009 3:08 PM
    Moderator
  • Can the sub.hta window reset the wshEnv.item before closing and the Master.hta get the new value?
    Monday, August 10, 2020 5:37 PM
  • Can the sub.hta window reset the wshEnv.item before closing and the Master.hta get the new value?

    No, because once the process closes the value of the environment variable is lost.


    -- Bill Stewart [Bill_Stewart]

    Monday, August 10, 2020 8:44 PM
    Moderator