IE用户名/密码提示不起作用

时间:2015-12-16 20:36:12

标签: javascript internet-explorer activex wsh

我正在尝试使用Internet Explorer构建用户名/密码提示弹出窗口,但我做错了。当我点击用户名字段时,光标几乎总是立即跳回密码字段,当我尝试打开服务器的下拉列表时,光标几乎总是立即跳回密码字段。我的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:&nbsp;" );
    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:&nbsp;" );
    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:&nbsp;&nbsp;" );
    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;
}

1 个答案:

答案 0 :(得分:0)

我能够通过在do while循环之外移动.focus()语句来解决问题。显然,每次代码循环时,焦点都会被强制回到密码输入字段。