IE username/password 提示不工作
IE username/password prompt not working
我正在尝试使用 Internet Explorer 构建一个 username/password 提示弹出窗口,但我做错了。当我单击“用户名”字段时,光标几乎总是立即跳回“密码”字段,而当我尝试打开服务器下拉列表时,光标几乎总是立即跳回“密码”字段。我的 Javascript 代码如下。任何建议将不胜感激。
var Version = "1.0";
var True = 1;
var False = 0;
var aryServers;
var intI, intLen;
var objWShell, objIE;
var strServer, strLoginID, strPassword, strLocalUser, strDefaultServer;
var blnPwdBoxWait;
aryServers = Array( "server1", "server2", "server3", "server4", "server5" );
objWShell = new ActiveXObject( "WScript.Shell" );
strLocalUser = objWShell.ExpandEnvironmentStrings( "%USERNAME%" );
blnPwdBoxWait = "";
strLoginID = "";
strPassword = "";
strDefaultServer = "server2";
if ( strPassword == "" ) {
strPassword = PasswordBox ( "Login_Box_Demo Version " + Version );
WScript.Sleep( 500 );
}
WScript.Echo( "LoginID: ", strLoginID, ", Password: ", strPassword, ", Server: ", strServer );
objWShell = null;
WScript.Quit( 0 );
// ===========================================================================
function keyHit(evt) {
var e = evt || window.event;
if ( e.keyCode == 13 ) {
blnPwdBoxWait = 'DONE';
}
return;
}
function PasswordBox( strIETitle ) {
objIE = new ActiveXObject( "InternetExplorer.Application" );
objIE.FullScreen = False;
objIE.AddressBar = False;
objIE.MenuBar = False;
objIE.StatusBar = False;
objIE.ToolBar = False;
objIE.RegisterAsDropTarget = False;
objIE.Navigate("about:blank");
strLoginID = strLocalUser;
do {
WScript.Sleep( 100 );
} while ( ! objIE.ReadyState == 4 );
objIE.document.parentWindow.resizeTo( 400, 300 + 70 );
objIE.document.parentWindow.resizeTo( 400, 200 + 70 );
objIE.document.parentWindow.moveTo(
objIE.document.parentWindow.screen.width / 2 - 200,
objIE.document.parentWindow.screen.height / 2 - 200 );
objIE.document.writeln( "<html>" );
objIE.document.writeln( "<head>" );
objIE.document.writeln( "<title>" + strIETitle + "</title>" );
objIE.document.writeln( "<style type='text/css'>" );
objIE.document.writeln( "<!--" );
objIE.document.writeln( ".fixed { font-family:courier new, monospace }" );
objIE.document.writeln( "-->" );
objIE.document.writeln( "</style>" );
objIE.document.writeln( "</head>" );
objIE.document.writeln( "<body bgcolor=Silver>" );
objIE.document.writeln( "<center>" );
objIE.document.writeln( "<form>" );
objIE.document.writeln( "<b>" + strIETitle + "</b><p>" );
objIE.document.writeln( "<table>" );
objIE.document.writeln( "<tr><td colspan=2 align=left>" );
objIE.document.writeln( "Enter your username and password:<br>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Username: " );
objIE.document.writeln( "</td><td>" );
objIE.document.writeln( "<input id='userid' size=20 class='fixed' " +
"value='" + strLoginID + "'>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Password: " );
objIE.document.writeln( "</td><td>" );
objIE.document.writeln( "<input type='password' id='passwd' size=20 class='fixed'><p>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Remote Host: " );
objIE.document.writeln( "</td><td valign=top>" );
objIE.document.writeln( "<select id='server'><br>" );
intLen = aryServers.length;
for ( intI = 0; intI < intLen; intI++ ) {
if ( strDefaultServer == aryServers[ intI ] ) {
objIE.document.writeln( "<option value='" + aryServers[ intI ] +
"' selected>" + aryServers[ intI ] + "<br>" );
} else {
objIE.document.writeln( "<option value='" + aryServers[ intI ] +
"'>" + aryServers[ intI ] + "<br>" );
}
}
objIE.document.writeln( "</select>" );
objIE.document.writeln( "</td></tr>" );
objIE.document.writeln( "</table>" );
objIE.document.writeln( "<p>" );
objIE.document.writeln( "<input type='button' value='Submit' id='but0' " +
"onclick=\"submitted.value='DONE';\">" );
objIE.document.writeln( "<input type='hidden' id='submitted' value=''>" );
objIE.document.writeln( "</form>" );
objIE.document.writeln( "</center>" );
objIE.document.writeln( "</body>" );
objIE.document.writeln( "</html>" );
objIE.document.parentWindow.document.body.scroll="no";
objIE.document.parentWindow.document.body.style.borderStyle = "outset";
objIE.document.parentWindow.document.body.style.borderWidth = "3px";
objIE.Visible = True;
objIE.document.addEventListener( "keydown", keyHit, false );
objWShell.AppActivate( strIETitle );
blnPwdBoxWait = '';
try {
do {
objIE.document.getElementById( "passwd" ).focus();
WScript.Sleep( 100 );
if ( objIE.Visible && blnPwdBoxWait == '' ) {
blnPwdBoxWait = objIE.document.getElementById( "submitted" ).value;
}
} while ( blnPwdBoxWait == '' );
} catch( err ) {
WScript.Echo('ERROR: ' + err.message);
blnPwdBoxWait = 'DONE';
}
strLoginID = objIE.document.getElementById( "userid" ).value;
strPassword = objIE.document.getElementById( "passwd" ).value;
strServer = objIE.document.getElementById( "server" ).options( objIE.document.getElementById( "server" ).selectedIndex ).text;
objIE.Visible = False;
objIE.Quit();
objIE = null;
return strPassword;
}
我能够通过将 .focus() 语句移到 do while 循环之外来解决问题。显然每次代码循环时焦点都被强制返回到密码输入字段。
我正在尝试使用 Internet Explorer 构建一个 username/password 提示弹出窗口,但我做错了。当我单击“用户名”字段时,光标几乎总是立即跳回“密码”字段,而当我尝试打开服务器下拉列表时,光标几乎总是立即跳回“密码”字段。我的 Javascript 代码如下。任何建议将不胜感激。
var Version = "1.0";
var True = 1;
var False = 0;
var aryServers;
var intI, intLen;
var objWShell, objIE;
var strServer, strLoginID, strPassword, strLocalUser, strDefaultServer;
var blnPwdBoxWait;
aryServers = Array( "server1", "server2", "server3", "server4", "server5" );
objWShell = new ActiveXObject( "WScript.Shell" );
strLocalUser = objWShell.ExpandEnvironmentStrings( "%USERNAME%" );
blnPwdBoxWait = "";
strLoginID = "";
strPassword = "";
strDefaultServer = "server2";
if ( strPassword == "" ) {
strPassword = PasswordBox ( "Login_Box_Demo Version " + Version );
WScript.Sleep( 500 );
}
WScript.Echo( "LoginID: ", strLoginID, ", Password: ", strPassword, ", Server: ", strServer );
objWShell = null;
WScript.Quit( 0 );
// ===========================================================================
function keyHit(evt) {
var e = evt || window.event;
if ( e.keyCode == 13 ) {
blnPwdBoxWait = 'DONE';
}
return;
}
function PasswordBox( strIETitle ) {
objIE = new ActiveXObject( "InternetExplorer.Application" );
objIE.FullScreen = False;
objIE.AddressBar = False;
objIE.MenuBar = False;
objIE.StatusBar = False;
objIE.ToolBar = False;
objIE.RegisterAsDropTarget = False;
objIE.Navigate("about:blank");
strLoginID = strLocalUser;
do {
WScript.Sleep( 100 );
} while ( ! objIE.ReadyState == 4 );
objIE.document.parentWindow.resizeTo( 400, 300 + 70 );
objIE.document.parentWindow.resizeTo( 400, 200 + 70 );
objIE.document.parentWindow.moveTo(
objIE.document.parentWindow.screen.width / 2 - 200,
objIE.document.parentWindow.screen.height / 2 - 200 );
objIE.document.writeln( "<html>" );
objIE.document.writeln( "<head>" );
objIE.document.writeln( "<title>" + strIETitle + "</title>" );
objIE.document.writeln( "<style type='text/css'>" );
objIE.document.writeln( "<!--" );
objIE.document.writeln( ".fixed { font-family:courier new, monospace }" );
objIE.document.writeln( "-->" );
objIE.document.writeln( "</style>" );
objIE.document.writeln( "</head>" );
objIE.document.writeln( "<body bgcolor=Silver>" );
objIE.document.writeln( "<center>" );
objIE.document.writeln( "<form>" );
objIE.document.writeln( "<b>" + strIETitle + "</b><p>" );
objIE.document.writeln( "<table>" );
objIE.document.writeln( "<tr><td colspan=2 align=left>" );
objIE.document.writeln( "Enter your username and password:<br>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Username: " );
objIE.document.writeln( "</td><td>" );
objIE.document.writeln( "<input id='userid' size=20 class='fixed' " +
"value='" + strLoginID + "'>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Password: " );
objIE.document.writeln( "</td><td>" );
objIE.document.writeln( "<input type='password' id='passwd' size=20 class='fixed'><p>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Remote Host: " );
objIE.document.writeln( "</td><td valign=top>" );
objIE.document.writeln( "<select id='server'><br>" );
intLen = aryServers.length;
for ( intI = 0; intI < intLen; intI++ ) {
if ( strDefaultServer == aryServers[ intI ] ) {
objIE.document.writeln( "<option value='" + aryServers[ intI ] +
"' selected>" + aryServers[ intI ] + "<br>" );
} else {
objIE.document.writeln( "<option value='" + aryServers[ intI ] +
"'>" + aryServers[ intI ] + "<br>" );
}
}
objIE.document.writeln( "</select>" );
objIE.document.writeln( "</td></tr>" );
objIE.document.writeln( "</table>" );
objIE.document.writeln( "<p>" );
objIE.document.writeln( "<input type='button' value='Submit' id='but0' " +
"onclick=\"submitted.value='DONE';\">" );
objIE.document.writeln( "<input type='hidden' id='submitted' value=''>" );
objIE.document.writeln( "</form>" );
objIE.document.writeln( "</center>" );
objIE.document.writeln( "</body>" );
objIE.document.writeln( "</html>" );
objIE.document.parentWindow.document.body.scroll="no";
objIE.document.parentWindow.document.body.style.borderStyle = "outset";
objIE.document.parentWindow.document.body.style.borderWidth = "3px";
objIE.Visible = True;
objIE.document.addEventListener( "keydown", keyHit, false );
objWShell.AppActivate( strIETitle );
blnPwdBoxWait = '';
try {
do {
objIE.document.getElementById( "passwd" ).focus();
WScript.Sleep( 100 );
if ( objIE.Visible && blnPwdBoxWait == '' ) {
blnPwdBoxWait = objIE.document.getElementById( "submitted" ).value;
}
} while ( blnPwdBoxWait == '' );
} catch( err ) {
WScript.Echo('ERROR: ' + err.message);
blnPwdBoxWait = 'DONE';
}
strLoginID = objIE.document.getElementById( "userid" ).value;
strPassword = objIE.document.getElementById( "passwd" ).value;
strServer = objIE.document.getElementById( "server" ).options( objIE.document.getElementById( "server" ).selectedIndex ).text;
objIE.Visible = False;
objIE.Quit();
objIE = null;
return strPassword;
}
我能够通过将 .focus() 语句移到 do while 循环之外来解决问题。显然每次代码循环时焦点都被强制返回到密码输入字段。