如何从vbscript中获取往返HTA的变量

发布于 2025-01-24 04:33:25 字数 2993 浏览 3 评论 0原文

更新:我找出了我第一个问题的答案。实际上,我使用了两种方法。对于“ 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

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. 如何将用户输入的值从UserID和密码传递给呼叫VBScript?

事先感谢您的所有指导和建议。

UPDATE: I figured out the answer to my first question. I used two methods, actually. For the "strBoxTitle" variable, I replaced the line:

<b>strBoxTitle</b><p>

with this

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

which correctly displayed the variable's value, rather than its name. This didn’t work for the "strLocalUser" variable, however, because it was inside an input tag. For that one, I added the line

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

to my window_OnLoad function, and changed the input tag from

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

to just

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

That also worked, filling in the user input field with the value of "strLocalUser", not to mention preventing me from needing to nest a script tag inside the input tag, which apparently is not allowed.

ORIGINAL QUESTION:

I'm trying to write an HTA file that gets called from a Vbscript file to get user input. The HTA file is called with the command

objWShell.Run ".\UserInput.hta | " & "Please Enter Your Credentials" & _
    " | " & strLoginID,1,True

The HTA file "UserInput.hta", which I wrote following examples here in Stack Overflow, is

<!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: 
        </td><td>
        <input id='userid' size=20 class='fixed' value=strLocalUser>
        </td></tr><tr><td valign=top>
        Password: 
        </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>

That correctly displays an input box with two input fields. Unfortunately, that's as far as I've gotten. My two questions are:

  1. How do I get the input box to display the values of the strBoxTitle and strLocalUser variables, instead of the text "strBoxTitle" and "strLocalUser"?
  2. How do I pass the user-entered values from the input boxes userid and password back to the calling Vbscript?

Thanks in advance for all guidance and suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2025-01-31 04:33:25

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

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 line将文档模式设置为ie = 9(仅使用!doctype设置HTA通常在IE = 7模式中)​​,2)添加Center -Window cance totexbar的功能使用availwidthavailheight,3)将重点放在密码字段上,以便用户可以立即开始键入,4)添加了of Enter 因此,用户不必使用鼠标,5)消除了在这种情况下不必要的form标签,并干扰了直接通过ID引用元素的能力,例如passwd.value ,6)用用户ID通过领先空间传递的问题,7)使用span标题文本(只是显示另一种方法)。

其他建议:删除冗余请求。按照书面形式,该接口具有,请输入您的凭据,然后请输入您的凭据,然后,请输入您的用户名和密码

Here's your script modified to pass the result back via the registry:

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

Other changes: 1) Added a meta line to set the document mode to IE=9 (with only !DOCTYPE set the HTA will usually be in IE=7 mode), 2) Added a CenterWindow function that accounts for taskbar using availWidth and availHeight, 3) Set focus on the password field so the user can immediately start typing, 4) Added a check for press of Enter so user doesn't have to use mouse, 5) Eliminated the form tag which is unnecessary in this context and interferes with ability to reference elements directly by id such as passwd.value, 6) fixed issue with userid being passed with a leading space, 7) used a span for the title text (just to show another way to do it).

Other suggestion: Remove redundant requests. As written, the interface has Please Enter Your Credentials, followed by Please Enter Your Credentials, followed by Please enter your username and password.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文