如何从 Vbscript 调用的 HTA 获取变量

How get variables to and from HTA called from Vbscript

更新: 我找到了第一个问题的答案。实际上,我使用了两种方法。对于“strBoxTitle”变量,我替换了行:

<b>strBoxTitle</b><p>

有了这个

<b>
<script>document.write( strBoxTitle )</script>
</b><p>

它正确显示了变量的值,而不是它的名称。但是,这对“strLocalUser”变量不起作用,因为它位于输入标签内。对于那个,我添加了行

document.getElementById("userid").value = strLocalUser

到我的 window_OnLoad 函数,并将输入标签从

更改为
<input id='userid' size=20 class='fixed' value=strLocalUser>

只是

<input id='userid' size=20 class='fixed'>

这也有效,用“strLocalUser”的值填充用户输入字段,更不用说防止我需要在输入标签内嵌套脚本标签,这显然是不允许的。

原问题:

我正在尝试编写一个从 Vbscript 文件调用以获取用户输入的 HTA 文件。使用命令

调用 HTA 文件
objWShell.Run ".\UserInput.hta | " & "Please Enter Your Credentials" & _
    " | " & strLoginID,1,True

我在 Stack Overflow 中编写的 HTA 文件“UserInput.hta”是

<!DOCTYPE html>
<html>
<head>
    <hta:application
        id = oHTA
        border = dialog
        innerborder = no
        caption = yes
        sysmenu = no
        maximizebutton = no
        minimizebutton = no
        scroll = no
        singleinstance = no
        showintaskbar = no
        contextmenu = no
        selection = yes
    />
    <style type='text/css'>
        .fixed { font-family:courier new, monospace }
    </style>
</head>
<script language="VBScript">
    CmdLine = oHTA.commandLine
    Params = Split(CmdLine,"|")
    strBoxTitle = Params( 1 )
    strLocalUser = Params( 2 )

    Sub window_OnLoad
        Document.Title = strBoxTitle
        Window.ResizeTo 400, 200 + 70
        Window.MoveTo Window.screen.width / 2 - 200, Window.screen.height / 2 - 200
    End Sub

    Function SubmitBtn()
        Window.Close
    End Function

</script>
<body bgcolor=Silver>
    <center><form>
        <b>strBoxTitle</b><p>
        <table>
        <tr><td colspan=2 align=left>
        Please enter your username and password:<br><br>
        </td></tr><tr><td valign=top>
        Username:&nbsp;
        </td><td>
        <input id='userid' size=20 class='fixed' value=strLocalUser>
        </td></tr><tr><td valign=top>
        Password:&nbsp;
        </td><td>
        <input type='password' id='passwd' size=20 class='fixed'><p>
        </td></tr>
        </table>
        <p>
        <input type='button' value='Submit' id='but0' onclick='SubmitBtn()'>
    </form></center>
</body>
</html>

正确显示了带有两个输入字段的输入框。不幸的是,就我所知。我的两个问题是:

  1. 如何让输入框显示 strBoxTitle 和 strLocalUser 变量的值,而不是文本“strBoxTitle”和“strLocalUser”?
  2. 如何将用户输入的值从输入框用户 ID 和密码传递回调用 Vbscript?

提前感谢所有指导和建议。

这是您修改后的脚本以通过注册表传回结果:

UserInput.hta

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
<hta:application
id = oHTA
border = dialog
innerborder = no
caption = yes
sysmenu = no
maximizebutton = no
minimizebutton = no
scroll = no
singleinstance = no
showintaskbar = no
contextmenu = no
selection = yes
>
<script language="VBScript">
Set oWSH = CreateObject("Wscript.Shell")
CmdLine = oHTA.commandLine
Params = Split(CmdLine,"|")
strBoxTitle = Params(1)
strLocalUser = Params(2)
CenterWindow 400,270

Sub window_OnLoad
  document.Title = strBoxTitle
  title.innerHTML = strBoxTitle
  userid.Value = strLocalUser
  passwd.focus
End Sub

Sub document_onKeyDown
  If window.event.keyCode=13 Then Submit
End Sub

Sub CenterWindow(w,h)
  Window.ResizeTo w,h
  Window.MoveTo (screen.availWidth - w)/2, (screen.availHeight - h)/2
End Sub

Sub Submit
  Key = "HKCU\Software\UserInputHTA\"
  oWSH.RegWrite Key & "UserName",userid.value
  oWSH.RegWrite Key & "Password",passwd.value
  Window.Close
End Sub
</script>

<style>
.fixed { font-family:courier new, monospace }
</style>
</head>
<body bgcolor=Silver>
<center>
<b><span id=title></span></b><br><br>
Please enter your username and password:<br><br>
<table>
<tr>
<td>Username:</td>
<td><input type=text id=userid size=20 class=fixed></td>
</tr>
<tr>
<td>Password:</td>
<td><input type=password id=passwd size=20 class=fixed></td>
</tr>
</table>
<br><br>
<input type=button value=Submit id=but0 onclick=Submit()>
</center>
</body>
</html>

TestUserInput.vbs

strLoginID = "SomeUser"
strPassword = ""
Set oWSH = CreateObject("Wscript.Shell")
oWSH.Run ".\UserInput.hta " & "|Please Enter Your Credentials|" & strLoginID,1,True
Key = "HKCU\Software\UserInputHTA\"
On Error Resume Next
strLoginID = oWSH.RegRead(Key & "UserName")
strPassword = oWSH.RegRead(Key & "Password")
oWSH.RegDelete Key
On Error Goto 0
MsgBox strLoginID & vbCRLF & strPassword

其他更改:1) 添加了 meta 行以将文档模式设置为 IE=9(只有 !DOCTYPE 设置 HTA 通常处于 IE=7 模式),2)添加了一个 CenterWindow 函数,该函数使用 availWidthavailHeight 来计算任务栏,3) 将焦点放在密码字段上,以便用户可以立即开始输入,4) 添加了对按下 Enter 所以用户不必使用鼠标,5) 消除了在此上下文中不必要的 form 标签,它会干扰通过 id 直接引用元素的能力,例如 passwd.value, 6)修复了 userid 与前导 space 一起传递的问题,7) 使用 span 作为标题文本(只是为了展示另一种方法)。

其他建议:删除多余的请求。如写的那样,界面有Please Enter Your Credentials,后面是Please Enter Your Credentials,后面是Please enter your username and password