2015-12-16 32 views
-1

我試圖建立一個用戶名/密碼提示彈出使用Internet Explorer,但我做錯了什麼。當我點擊用戶名字段時,光標幾乎總是立即跳回到密碼字段,當我嘗試打開服務器下拉列表時,光標幾乎總是立即跳回密碼字段。我的Javascript代碼如下。任何建議將不勝感激。IE用戶名/密碼提示不起作用

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; 
} 

回答

0

我能夠通過移動do while循環之外的.focus()語句來解決問題。顯然,每次代碼循環時,重點都被迫回到密碼輸入字段。

相關問題