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="setFocus();" onkeydown="keyPress();">
<form name="form">
<p>Please enter the password:
<input type="password" name="Password" size="20">
<input type="button" value="Open" name="Button" onclick="Verify();"></p>
</form>
</body>
<script LANGUAGE="JavaScript">
<!--
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() + ".htm";
if (PageName.length == 11) {
this.location.href(PageName);
}
else {
this.location.href("error.htm"
}
}
//-->
</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>
</head>
<body onload="setFocus();" onkeydown="keyPress();">
<form name="form">
<p>Please enter the password:
<input type="password" name="Password" size="20">
<input type="button" value="Open" name="Button" onclick="Verify();"></p>
</form>
</body>
<script LANGUAGE="JavaScript">
<!--
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() + ".htm";
if (PageName.length == 11) {
this.location.href(PageName);
}
else {
this.location.href("error.htm"
}
}
//-->
</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