html - My Javascript function to make an Internet Explorer prompt window works when I hit the Submit button but not the Enter key -
update: got working. added keypress event handler:
function keyhit(evt) { var e = evt || window.event; if ( e.keycode == 13 ) { blnpwdboxwait = 'done'; } return; } and add following line password prompt function:
objie.document.addeventlistener( "keydown", keyhit, false ); the loop waits happen looks this:
try { { wscript.sleep( 100 ); if ( objie.visible && blnpwdboxwait == '' ) { blnpwdboxwait = objie.document.getelementbyid( "submitted" ).value; } } while ( blnpwdboxwait == '' ); } catch( err ) { wscript.echo('error: ' + err.message); blnpwdboxwait = 'done'; } what caused me confusion fact proved (with use of wscript.echo statements) event handler catching keystrokes, loop wasn't exiting. realized needed change line
if ( objie.visible ) { to
if ( objie.visible && blnpwdboxwait == '' ) { to keep flag variable blnpwdboxwait being overwritten '' when enter key pressed submit button had not been clicked. once made fix, worked charm. help!
original post: have javascript function uses internet explorer open window prompts user login id, password, , target server, , displays "submit" button. works, doesn't when enter key pressed, when "submit" button clicked. tried usual things adding action attribute form tag, nothing i've tried seems work. can tell me should make function submit values when enter key pressed? here's small demo containing function:
true = 1; false = 0; aryservers = array( "server1", "server2", "server3", "server4" ); strdefaultserver = "server2"; objwshell = new activexobject( "wscript.shell" ); strlocaluser = objwshell.expandenvironmentstrings( "%username%" ); strpassword = passwordbox( "enter credentials" ); wscript.echo( "login id: " + strloginid + "\npassword: " + strpassword + "\nserver: " + strserver ); wscript.exit; // =========================================================================== 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; { 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 username , 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; ( 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.document.getelementbyid( "passwd" ).focus(); objie.visible = true; objwshell.appactivate( strietitle ); blnpwdboxwait = ''; try { { wscript.sleep( 100 ); if ( objie.visible ) { 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; }
Comments
Post a Comment