Hi Lasse,
thanks for the detailed information. It was very useful!
I tried the first suggestion you gave me, and changed my function this way:
<script type='text/javascript'>
function NoEnter(event) {
event = event || window.event;
if (event.keyCode == 13) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
}
</script>
I tried the modified code on IE 6 and it worked. Then I tried on Netscape 6
and 7, and it didn't work. I am calling the function as before:
<input type="text" maxlength="13" id="txtType" onKeyPress="NoEnter();" />
Can you help me one more time?
Thanks,
Robert Scheer
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<r8**********@hotpop.com>...
rb******@my-deja.com (Robert Scheer) writes:
I wrote this function to avoid the enter key in some of my textboxes.
In IE the function works as expected, in Netscape 6 or 7 it does not
work.
There are lots of reasons for that (i.e., lots of IE specific coding).
How can I make my function works in both IE and Netscape.
<input type="text" maxlength="13" id="txtType" onKeyPress="NoEnter();"
/>
<Script language='Javascript'>
<script type="text/javascript">
The type attribute is mandatory, and the language attribute is
deprecated, in HTML 4.
function NoEnter()
function NoEnter(event)
IE has a global event. Other browsers follow the DOM standard and
take the event as an argument of the handler.
{
That means that we have to take special care of IE. I start almost
all my handlers with the following line:
event = event || window.event; // IE sucks
(yes, I write the comment every time too).
if (window.event.keyCode == 13)
This then becomes
if (event.keyCode == 13)
{
window.event.cancelBubble = true;
This one is irrelevant (and IE specific).
window.event.returnValue = false;
This one is IE specific. The standard DOM version (with a wrapper
to avoid trouble with older IE's) is:
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false; // IE sucks
}
}
}
</Script>
In summary:
---
<script type="text/javscript>
function NoEnter(event)
event = event || window.event; // IE sucks
if (event.keyCode == 13)
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false; // IE sucks
}
}
}
</script>
---
Since you call the function from an HTML handler, you could just have
the NoEnter function return true or false, and then cancel the event
with the return value of the text handler:
<input type="text" maxlength="13" id="txtType"
onKeyPress="return NoEnter();" />
(where I added "return") and
<script type="text/javascript">
function NoEnter(event) {
event = event || window.event; // IE sucks
return (event.keyCode != 13);
}
</script>
/L