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:
Thanks in advance for all guidance and suggestions.
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
.