0

For several years now, I've been using the Javascript function below to have Internet Explorer generate a username and password prompt by using ActiveX. Now that Internet Explorer is about to be retired, does anyone know how I can accomplish the same thing using Edge, which doesn't support ActiveX (or by using Firefox or Chrome or...)? Thanks to all who respond.

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, 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>" );
    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 );
    objIE.document.getElementById( "passwd" ).focus();
    blnPwdBoxWait = '';
    try {
        do {
            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;
    objIE.Visible = False;
    objIE.Quit();
    objIE = null;

    return strPassword;
}
7
  • Assuming you do not want to build something from scratch you need to search for html/javascript modal dialog. An example of one (with code) is at the w3.com site at w3.org/TR/wai-aria-practices-1.1/examples/dialog-modal/…
    – Mifo
    Commented Apr 18, 2022 at 20:49
  • I think you have two choices. One is loading the site in IE mode in Edge. IE mode in Edge supports ActiveX controls. Second is changing your code to JavaScript without ActiveX. Just like the comment above, you can use Modal Dialog to show the username and password prompt.
    – Yu Zhou
    Commented Apr 19, 2022 at 8:00
  • Mifo - thanks for the suggestion, but since my function uses "writeln" commands to create the HTML code that IE is reading, it isn't clear to me how modal would parse the commands, not to mention the code that formats the IE window being opened.
    – Leslie
    Commented Apr 20, 2022 at 19:21
  • Yu Zhou - thanks for the idea, but since IE is being invoked by the "objIE = new ActiveXObject( "InternetExplorer.Application" )" command, it isn't clear to me how I could change that command to invoke Edge in IE mode.
    – Leslie
    Commented Apr 20, 2022 at 19:23
  • IE mode supports ActiveX controls. You only need to load the website executing the JavaScript function in IE mode, then the ActiveX controls can run and the newly generated page will open in IE.
    – Yu Zhou
    Commented Apr 22, 2022 at 7:30

0