C
coolsti
Can someone help me enhance this code snippet which works only for IE so
that it will also work for Firefox? I have been trying all sorts of things
and have gotten to where I can capture the keydown and keypress events in
Firefox, but then I can't seem to get the key code. I also don't know if
the window.event.srcElement.type works with Firefox or if the
onkeydown="return false" is valid in Firefox.
I have this Javascript at the end of the header of my page, and it is a
function that should look at which key was pressed no matter where the
user is on the page. If the event was triggered by an input field of type
"text", the Enter key is deactivated. If the event is triggered by a
"textarea" input field, nothing is deactivated. In all other cases, both
the Enter and Backspace keys are deactivated.
<html><head>
<!-- various header code here -->
<script type="text/javascript">
function killReturn(e) {
if ((e.keyCode != 13) & (e.keyCode != 8)) { return true;}
switch (window.event.srcElement.type) {
case 'textarea':
// Deactivate nothing
return true;
case 'text':
// Deactivate Enter key
if (e.keyCode == 13) {
return false;
}
return true;
default:
// Deactivate both Enter and Backspace keys
return false;
}
}
</script>
</head>
<body>
<form name="form1" method="post" onkeydown="return killReturn(event);"
action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!-- rest of html page from here ... -->
As mentioned, the above works fine in Internet Explorer.
Thanks for any help!
Regards,
Steve, Denmark
that it will also work for Firefox? I have been trying all sorts of things
and have gotten to where I can capture the keydown and keypress events in
Firefox, but then I can't seem to get the key code. I also don't know if
the window.event.srcElement.type works with Firefox or if the
onkeydown="return false" is valid in Firefox.
I have this Javascript at the end of the header of my page, and it is a
function that should look at which key was pressed no matter where the
user is on the page. If the event was triggered by an input field of type
"text", the Enter key is deactivated. If the event is triggered by a
"textarea" input field, nothing is deactivated. In all other cases, both
the Enter and Backspace keys are deactivated.
<html><head>
<!-- various header code here -->
<script type="text/javascript">
function killReturn(e) {
if ((e.keyCode != 13) & (e.keyCode != 8)) { return true;}
switch (window.event.srcElement.type) {
case 'textarea':
// Deactivate nothing
return true;
case 'text':
// Deactivate Enter key
if (e.keyCode == 13) {
return false;
}
return true;
default:
// Deactivate both Enter and Backspace keys
return false;
}
}
</script>
</head>
<body>
<form name="form1" method="post" onkeydown="return killReturn(event);"
action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!-- rest of html page from here ... -->
As mentioned, the above works fine in Internet Explorer.
Thanks for any help!
Regards,
Steve, Denmark