/    Sign up×
Community /Pin to ProfileBookmark

Javascript to make IE input window works when I click Submit but not when I hit Enter

I have a Javascript function that uses Internet Explorer to open a window that prompts a user for his login ID, password, and target server, and displays a “Submit” button. It works, but it doesn’t do anything when the Enter key is pressed, only when the “Submit” button is clicked. I tried the usual things like adding an action attribute to the form tag and changing “<input type=’button’…” to “<input type=’submit’…”, but nothing I’ve tried seems to work. Can anyone tell me what I should do to make the function submit its values when the Enter key is pressed as well as when the “Submit” button is clicked? Thanks in advance to all who respond. Here’s a small demo containing the function:

[CODE]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;

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.document.getElementById( “passwd” ).focus();
objIE.Visible = True;

objWShell.AppActivate( strIETitle );

blnPwdBoxWait = ”;
try {
do {
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;
}[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@wbportJun 08.2015 — You need to create a hit key event. Check the top of this timesheet page for ideas, lines 11 - 37. The value of the return key is 13. The key hit event will have a LOT of traffic so be sure your Submit is not kicked off when you don't want it to.
Copy linkTweet thisAlerts:
@lhoukauthorJun 09.2015 — You need to create a hit key event. Check the top of this timesheet page for ideas, lines 11 - 37. The value of the return key is 13.[/QUOTE]
Thanks for your response. I looked at the page you suggested, and added a small function to create the event (and a line to invoke it), but it's still not working. The new version of my program is below; do you see what I got wrong? Again, thanks.

[CODE]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 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.onkeydown = keyHit; // LINE TO CALL NEW FUNCTION

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 type='text' 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.document.getElementById( "passwd" ).focus();
objIE.Visible = True;

objWShell.AppActivate( strIETitle );

blnPwdBoxWait = '';
try {
do {
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;
}[/CODE]
Copy linkTweet thisAlerts:
@wbportJun 10.2015 — Thanks for your response. I looked at the page you suggested, and added a small function to create the event (and a line to invoke it), but it's still not working. The new version of my program is below; do you see what I got wrong? Again, thanks.

[CODE]
function keyHit(evt) {
var e = evt || window.event;
if (e.keyCode == 13) {
blnPwdBoxWait = 'DONE';
}
return;
}[/CODE][/QUOTE]

You need to be doing or performing the same thing on keyCode 13 as you do when someone clicks on [b]Submit[/b]. Also, is there anywhere else on your page where the user would press [B]Enter[/B] where you would not want this to be done? I use something similar on my trivia page where sometimes the Enter key forces focus down to the next line and other places where it has no effect.

HTH
Copy linkTweet thisAlerts:
@lhoukauthorJun 10.2015 — I got it working. I added the keypress event handler:

[CODE]function keyHit(evt) {
var e = evt || window.event;
if ( e.keyCode == 13 ) {
blnPwdBoxWait = 'DONE';
}
return;
}[/CODE]


and add the following line to my password prompt function:

[CODE]objIE.document.addEventListener( "keydown", keyHit, false );[/CODE]

The loop that waits for something to happen now looks like this:

[CODE]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';
}[/CODE]


What caused me the most confusion was the fact that I proved (with the use of WScript.Echo statements) that the event handler was catching keystrokes, but the loop wasn't exiting. I finally realized that I needed to change the line

[CODE]if ( objIE.Visible ) {[/CODE]

to

[CODE]if ( objIE.Visible && blnPwdBoxWait == '' ) {[/CODE]

to keep the flag variable blnPwdBoxWait from being overwritten to '' when the Enter key was pressed but the Submit button had not been clicked. Once I made that fix, it worked like a charm. Thanks for your help!
Copy linkTweet thisAlerts:
@wbportJun 10.2015 — Glad I could help.

This was the first time I had seen [B]WScript[/B] outside of user tools started in a music notation program, this is the end of a typical script:

[CODE]var myLines=calculate(WScript.StdIn.ReadAll()).join("rn");
if (rc == 0)
WScript.StdOut.Write(myLines);
else
WScript.StdErr.Write(errMsg);
WScript.quit(rc);[/CODE]


When is it necessary to use [B]WScript[/B] constructs?
Copy linkTweet thisAlerts:
@lhoukauthorJun 11.2015 — Glad I could help.

This was the first time I had seen [B]WScript[/B] outside of user tools...

When is it necessary to use [B]WScript[/B] constructs?[/QUOTE]

I suppose it wasn't really necessary to use them in this case. It's just that I cut my teeth writing DOS batch files, and WScript was what I started using when I wanted to write the same type of scripts in Windows. It's just more familiar to me than JavaScript and other scripting languages. So, when I wanted a quick and dirty "Hello world! You are at this point in the script!" debug output statement on the console, I used WScript.Echo out of habit. ?
×

Success!

Help @lhouk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...