473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Avoi enter key in IE and Netscape

Hi.

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. How can I make my function works in both IE and Netscape.

<input type="text" maxlength="13" id="txtType" onKeyPress="NoEnter();"
/>

<Script language='Javascript'>
function NoEnter()
{
if (window.event.keyCode == 13)
{
window.event.cancelBubble = true;
window.event.returnValue = false;
}
}
</Script>

Thanks,

Robert Scheer
Jul 20 '05 #1
1 6459
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

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: |-|erc | last post by:
these 3 lines will put the following into the URL bar! but with 2 input boxes it doesn't. <form name=test> <INPUT name='selqty2'> </form> file:///C:/WINDOWS/DESKTOP/tttt2.html?selqty2=55555
7
by: JCO | last post by:
How's come when I press the enter key, I can't get it to execute the correct password. It seems that I'm forced to press the button. I want to be able to do both. How is this done?
1
by: Stephen | last post by:
Hi, The problem is like this, I have a textbox and a button, I wanted to disable the enter key, so as to prevent the user from hitting the enter and seeing undesirable results. I used this...
1
by: Matthew Wieder | last post by:
Hi - I wanted to capture the enter button on a form since I have a datagrid with the first column being a delete button and if someone hits enter it deletes the first record. I coded: private...
5
by: Girish | last post by:
I have TWO submit buttons of type IMAGE on my asp form. This renders as <input type="image">. I need to be able to eble the ENTER button for both buttons. Yes, I know that for the input type...
1
by: Yobbo | last post by:
Hi All I use the following script to stop users typing in anything but standard chars (eg letters, numbers, etc) into a input textbox: <!-- // 8 = backspace // 9 = tab // 46 = del // 190 =...
0
by: polesh | last post by:
I have a regular html textbox and button. When the user presses "Enter" from the keyboard instead of clicking the "Go" button I would like the code that would have run by clicking the button work...
3
by: pvrkraju | last post by:
hai, I am using html:textarea and when i enter text in these textarea in a single line, the text is not getting wrapped in netscape browser. As it was giving a horizontal bar and the text which i...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.