Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<!-- saved from url=(0053)http://msdn.microsoft.com/en-us/library/ms537628.aspx -->
<head><title>Input Credentials</title></head>
<SCRIPT LANGUAGE='VBScript'>
Sub RunScript
OKClicked.Value = "OK"
End Sub
Sub CancelScript
OKClicked.Value = "Cancelled"
End Sub
</SCRIPT>
<SCRIPT LANGUAGE='JavaScript'>
function CheckKey($key) {
if ($key == 13) {
RunScript();
}
}
</SCRIPT>
<BODY>
<table border=0>
<tr>
<td>UserName:</td>
<td><input type='text' name='UserName'></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='UserPassword' onKeyPress='CheckKey(window.event.keyCode)'></td>
</tr>
<tr>
<td colspan='2'>
<center>
<input id=runbutton class='button' type='button' value=' OK ' name='ok_button' onClick='RunScript'>
<input id=runbutton class='button' type='button' value='Cancel' name='cancel_button' onClick='CancelScript'>
</center>
</td>
</tr>
</table>
<input type='hidden' name='OKClicked' size = '20'>
</BODY>
[green]
'===============================================================================
'
' NAME: CredentialsSub.vbs
'
' AUTHOR: P. S. Chapman
'
' DATE: 5/26/2008
'
' COMMENT: Script creates an IE based credentials input window. Masks user's
' password.
'
' sUserName and sPassword are passed to sub "ByRef" and are modified by the sub.
' This allows the script to use less memory space by not copying the data from
' variable to variable.
'
' HTM file is permitted to run scripts due to "Mark of the Web" placed at the top.
' Mark of the Web can be any URL. The one in this script refers to Microsoft's
' MSDN page describing its use.
'
'===============================================================================[/green]
Option Explicit
[green]' Declare variables[/green]
Dim sUserName, sPassword, oShell
[green]' Create Shell object (used to bring IE to the foreground later)[/green]
Set oShell = CreateObject("WScript.Shell")
[green]' Call subroutine, passing variables that will be used/modified by the sub[/green]
Call GetPWInfo(sUserName, sPassword, oShell)
MsgBox "UserName: " & sUserName & VbCrLf & "Password: " & sPassword
[blue]Sub[/blue] GetPWInfo(ByRef sUserName, ByRef sPassword, oShell)
Dim oIEPW, sPWHTML, ctr
[green]' Get HTML and place it in a variable[/green]
Call WriteHTML(sPWHTML)
[green]' Create Internet Explorer object[/green]
Set oIEPW = WScript.CreateObject("InternetExplorer.Application", "IE_")
[green]' Set window properties[/green]
With oIEPW
.Navigate "about:blank" [green]' Use blank page as base[/green]
Do While .ReadyState <> 4 [green]' Wait for IE process to start[/green]
WScript.Sleep 50
Loop
.Document.Open [green]' Create document that will be in IE window[/green]
.Document.Write (sPWHTML) [green]' Paste in HTML[/green]
.Document.Close [green]' Complete the page creation[/green]
.ToolBar = 0 [green]' Disable IE's tool bars[/green]
.StatusBar = 0 [green]' Disable IE's status bar[/green]
.Width = 325 [green]' Set window width in pixels[/green]
.Height = 160 [green]' Set window height in pixels[/green]
.Left = 72 [green]' Distance in pixels of window left edge to left side of screen[/green]
.Top = 72 [green]' Distance in pixels of window top edge to top of screen[/green]
Do While .ReadyState <> 4 [green]' Wait for all properties to be set[/green]
WScript.Sleep 50
Loop
.Visible = 1 [green]' Make IE window visible[/green]
End With
[green]' Bring IE window to the top[/green]
oShell.AppActivate "Input Credentials"
[green]' Enter a loop to wait for user to enter valid credentials or hit cancel[/green]
Do
On Error Resume Next
[green]' Monitor IE window for a change in value of the "OKClicked" hidden input[/green]
Do While (oIEPW.Document.Body.All.OKClicked.Value = "")
[green]' If Window is manually closed by user an error will be generated, exit sub[/green]
If Err <> 0 Then
[green]' User forcibly closed window, so set UserName and Password
' to "cancelled" for calling code to parse[/green]
sUserName = "cancelled"
sPassword = "cancelled"
Exit Sub
End If
Wscript.Sleep 250
Loop
On Error GoTo 0
[green]' Check OKClicked value for OK or CANCELED[/green]
If oIEPW.Document.Body.All.OKClicked.Value = "OK" Then
[green]' Verify user input a value in the UserName input box[/green]
If oIEPW.Document.Body.All.UserName.Value <> "" Then
[green]' Capture UserName and Password values[/green]
sUserName = oIEPW.Document.Body.All.UserName.Value
sPassword = oIEPW.Document.Body.All.UserPassword.Value
[green]' When ctr is set to 1, the script will be able to exit the loop[/green]
ctr = 1
Else
[green]' User failed to put in a user name, modify the HTML to make UserName text appear red[/green]
sPWHTML = Replace(sPWHTML,_
"<td>UserName:</td>", _
"<td><font color='red'>UserName: </font></td>", _
1, 1, vbbinarycompare)
[green]' Change the HTML document and update window[/green]
With oIEPW
.Document.Open
.Document.Write (sPWHTML)
.Document.Close
End With
[green]' Bring IE window back to the foreground[/green]
oShell.AppActivate "Input Credentials"
End If
Else
[green]' User clicked CANCEL, so set UserName and Password
' to "cancelled" for calling code to parse[/green]
sUserName = "cancelled"
sPassword = "cancelled"
[green]' Set ctr to 1 so that code can get out of loop[/green]
ctr = 1
End If
Loop Until ctr = 1
[green]' Shut down the IE process and associated window[/green]
oIEPW.Quit
[blue]End Sub
Sub[/blue] WriteHTML(ByRef sPWHTML)
[green]' ByRef passing of variable allows script to use less memory[/green]
sPWHTML = _
"<!-- saved from url=(0054)http://msdn2.microsoft.com/en-us/library/ms537628.aspx -->" & vbcrlf & _
"<head><title>Input Credentials</title></head>" & vbcrlf & _
"<SCRIPT LANGUAGE='VBScript'>" & vbcrlf & _
" Sub RunScript" & vbcrlf & _
" OKClicked.Value = ""OK""" & vbcrlf & _
" End Sub" & vbcrlf & _
" Sub CancelScript" & vbcrlf & _
" OKClicked.Value = ""Cancelled""" & vbcrlf & _
" End Sub" & vbcrlf & _
"</SCRIPT>" & vbcrlf & _
"<SCRIPT LANGUAGE='JavaScript'>" & vbcrlf & _
" function CheckKey($key) {" & vbcrlf & _
" if ($key == 13) {" & vbcrlf & _
" RunScript();" & vbcrlf & _
" }" & vbcrlf & _
" }" & vbcrlf & _
"</SCRIPT>" & vbcrlf & _
"<BODY>" & vbcrlf & _
" <table border=0>" & vbcrlf & _
" <tr>" & vbcrlf & _
" <td>UserName:</td>" & vbcrlf & _
" <td><input type='text' name='UserName'></td>" & vbcrlf & _
" </tr>" & vbcrlf & _
" <tr>" & vbcrlf & _
" <td>Password:</td>" & vbcrlf & _
" <td><input type='password' name='UserPassword' onKeyPress='CheckKey(window.event.keyCode)'></td>" & vbcrlf & _
" </tr>" & vbcrlf & _
" <tr>" & vbcrlf & _
" <td colspan='2'>" & vbcrlf & _
" <center>" & vbcrlf & _
" <input id=runbutton class='button' type='button' value=' OK ' name='ok_button' onClick='RunScript'>" & vbcrlf & _
" " & vbcrlf & _
" <input id=runbutton class='button' type='button' value='Cancel' name='cancel_button' onClick='CancelScript'>" & vbcrlf & _
" </center>" & vbcrlf & _
" </td>" & vbcrlf & _
" </tr>" & vbcrlf & _
" </table>" & vbcrlf & _
" <input type='hidden' name='OKClicked' size = '20'>" & vbcrlf & _
"</BODY>"
[blue]End Sub[/blue]