Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pressing Enter Key Instead of Clicking Button 1

Status
Not open for further replies.

pnjones

Technical User
Dec 1, 2000
25
US
I know this question has been posed several times before, but I cannot get this to work. I am trying to allow the user to type a password, click a button called "Open" OR press the Enter key to link to another page. For simplicity I have created a page (see code below) which illustrates the idea and recreates the problem. Please try it at using Internet Explorer. The correct password is SUCCESS.

<html>
<head>
<title>Test Using the Enter Key</title>
</head>
<body onload=&quot;setFocus();&quot; onkeydown=&quot;keyPress();&quot;>
<form name=&quot;form&quot;>
<p>Please enter the password: 
<input type=&quot;password&quot; name=&quot;Password&quot; size=&quot;20&quot;>  
<input type=&quot;button&quot; value=&quot;Open&quot; name=&quot;Button&quot; onclick=&quot;Verify();&quot;></p>
</form>
</body>
<script LANGUAGE=&quot;JavaScript&quot;>
<!--
function setFocus() {
document.form.Password.focus();
}
function keyPress() {
if (window.event.keyCode == 13)
document.form.Button.click();
}
function Verify() {
var PageName = document.form.Password.value.toLowerCase() + &quot;.htm&quot;;
if (PageName.length == 11) {
this.location.href(PageName);
}
else {
this.location.href(&quot;error.htm&quot;);
}
}
//-->
</script>
</html>

Here is what I have observed:

(1) Clicking the Open button always works.
(2) If I run this page from my C: drive the Enter key works fine, but if I run it from a web server, the page appears to link to itself (you can see this by clicking the browser's Back button after the response from pressing the Enter key).
(3) By placing 'alert' statements before the 'this.location.href' statements I can tell that it is recognizing that the Enter key has been pressed and is following the intended path.
(4) If I place 'alert' statements AFTER the 'this.location.href' statements it works like it is supposed to.

It almost appears to be some sort of timing problem.

Any suggestions would be appreciated.

Thanks,
Paul Jones
 
<html>
<head>
<title>Test Using the Enter Key</title>
<script LANGUAGE=&quot;JavaScript&quot;>
<!--
function setFocus() {
document.form.Password.focus();
}
function Verify() {
var PageName = document.form.Password.value.toLowerCase() + &quot;.htm&quot;;
if (PageName.length == 11) {
this.location.href(PageName);
}
else {
this.location.href(&quot;error.htm&quot;);
}
}
//-->
</script>
</head>
<body onload=&quot;setFocus()&quot;>
<form name=&quot;form&quot; onsubmit=&quot;Verify()&quot;>
<p>Please enter the password:
<input type=&quot;password&quot; name=&quot;Password&quot; size=&quot;20&quot;>
<input type=&quot;submit&quot; value=&quot;Open&quot; name=&quot;Button&quot;></p>
</form>
</body>
</html>
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Hi Jemminger,

Did you test it locally from your hard drive or did you go to the link ? The original code does work OK when run from the hard drive but not from a web server. I have tested it with IE 5.0, 5.5, 6.0, Opera 6, Netscape 4.7, Netscape 6, and Mozilla 1.0 ... always with the same results.

NEVERSLEEP, thanks for the try. I am sure you are onto something, but I copied and pasted your code as-is and it doesn't seem work at all (Enter key or Open button). See
Thanks,
Paul Jones
 
<!-- try this then -->
<html>
<head>
<title>Test Using the Enter Key</title>
<script LANGUAGE=&quot;JavaScript&quot;>
<!--
function Verify() {
var PageName = document.form.Password.value.toLowerCase() + &quot;.htm&quot;;
if (PageName.length == 11) {
top.location.href(PageName);
} else {
top.location.href(&quot;error.htm&quot;);
}
}
//-->
</script>
</head>
<body onload=&quot;document.form.Password.focus()&quot;>
<form name=&quot;form&quot; action=&quot;javascript:Verify()&quot;>
<p>Please enter the password:
<input type=&quot;password&quot; name=&quot;Password&quot; size=&quot;20&quot;>
<input type=&quot;submit&quot; value=&quot;Open&quot;></p>
</form>
</body>
</html>
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
pnjones - I tried neversleep's first solution (from your page) and it worked perfectly... ie5.5

good luck Chris says: &quot;It's time for a beer.&quot;
 
Thanks NEVERSLEEP,

Yes, your second version did the trick. It works in IE 5.0, 5.5, 6.0, Opera 6, Netscape 6, and Mozilla 1.0. Unfortunately in Netscape 4.7 it now doesn't respond to Enter or the Open button.

Do you have any thoughts on how to get it to work in Netscape 4? I would settle for just the Open button.

And I agree with Chris ... it is time for a beer!

Thanks again,
Paul Jones
 
&quot;Do you have any thoughts on how to get it to work in Netscape 4?&quot;

[flush] Netscrapy 4 ....sorry about that
but &quot;works on ... not on ...&quot; just [flame] me
why can they make it all the same ... [curse]

what the error in netscrapy 4 ?
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
No error in netscrapy 4. It just doesn't respond at all when you click the Open button or press the Enter key. I tried adding <b>onclick=&quot;javascript:Verify()&quot;</b> to the submit button's <input> tag but that didn't help.

Paul Jones
 
I developed a workaround. It is crude but it works. I just replaced the entire <form> with JavaScript to write a different version of the form for Netscape 4. Otherwise it uses the NEVERSLEEP version:

<script LANGUAGE=&quot;JavaScript&quot;>
if ( document.layers ) {
document.writeln(&quot;<form name='form'>&quot;);
document.writeln(&quot;<p>Please enter the password:&quot;);
document.writeln(&quot;<input type='password' name='Password' size='20'>&quot;);
document.writeln(&quot;<input type='button' value='Open' onclick='javascript:Verify();'></p>&quot;);
document.writeln(&quot;</form>&quot;);
}
else {
document.writeln(&quot;<form name='form' action='javascript:Verify();'>&quot;);
document.writeln(&quot;<p>Please enter the password:&quot;);
document.writeln(&quot;<input type='password' name='Password' size='20'>&quot;);
document.writeln(&quot;<input type='submit' value='Open'></p>&quot;);
document.writeln(&quot;</form>&quot;);
}
</script>

Netscape 4 won't respond to the Enter key but it will respond to the Open button. I guess 99.9% of the user community will have the convenience of using the Enter key and if anyone complains I will just suggest an upgrade.

Thanks to all who took the time to look into my problem.

Paul Jones


 
A FINAL WORD: After persisting a little more I was able to get the NEVERSLEEP version to work with Netscape 4 also. It just needed method=&quot;post&quot; on the <form> tag. Everything works now, the Enter key and the Open button in all browsers (at least the ones that I have access to). Here is the final code:

<html>
<head>
<title>Test Using the Enter Key</title>
<script LANGUAGE=&quot;JavaScript&quot;>
<!--
function Verify() {
var PageName = document.form.Password.value.toLowerCase() + &quot;.htm&quot;;
if (PageName.length == 11) {
self.location.replace(PageName);
} else {
self.location.replace(&quot;error.htm&quot;);
}
}
//-->
</script>
</head>
<body onload=&quot;document.form.Password.focus();&quot;>
<form name=&quot;form&quot; action=&quot;javascript:Verify();&quot; method=&quot;post&quot;>
<p>Please enter the password:
<input type=&quot;password&quot; name=&quot;Password&quot; size=&quot;20&quot;>
<input type=&quot;submit&quot; value=&quot;Open&quot;></p>
</form>
</body>
</html>

Paul Jones


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top