473,385 Members | 1,661 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,385 software developers and data experts.

How to handle input OnKeyPress in FireFox?

nicebasic
I have this code:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="message" id=message size="76">
  2. &nbsp;
  3. <input type="submit" value="Send" name="B1" onclick="alert('demo')">
In Internet Explorer, if you press ENTER while the inputbox or the button has got the focus, an Alert will be displayed. So, it works fine in IE.

But in Firefox, you can see the Alert only if you press the button.

How can I fix this bug in Firefox?

Thank you.
Apr 17 '12 #1

✓ answered by Dormilich

IE (IIRC before 9) uses a different event model and does not pass the event object to the handler function but uses a global object: window.event.

9 6496
Inspired by the following page:
http://stackoverflow.com/questions/6...ct-pressed-key

I have changed the code like this:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function getEnterKey(evt)
  3. {
  4.     var charCode = (evt.which) ? evt.which : evt.keyCode
  5.     if (charCode == 13)
  6.     {
  7.         sendIt();
  8.         //return true;
  9.     }
  10. }
  11. </script>
  12. <input type="text" name="message" id=message size="76" onKeyDown="return getEnterKey(event);">
  13. &nbsp;
  14. <input type="submit" value="Send" name="B1" onclick="alert('demo')">
  15.  
It works properly in Firefox now, but unfortunately it doesn't seem to work in IE.

What's wrong with this?

Any help or suggestion will be appreciated.
Apr 17 '12 #2
Dormilich
8,658 Expert Mod 8TB
IE (IIRC before 9) uses a different event model and does not pass the event object to the handler function but uses a global object: window.event.
Apr 18 '12 #3
Can you help me change this script to make it compatible with both IE and FF?

I'll be really grateful to you.
Apr 18 '12 #4
Dormilich
8,658 Expert Mod 8TB
it depends on how far you want to go back (IE 10 .. 6) to support it. generally, as of IE9 DOM Events are supported.
Apr 18 '12 #5
I'd like it to be compatible with all major browsers one of which is IE. For IE, as many versions as possible is the answer.

I hope you can give me a solution.

I have searched a lot and found very little to help me.

Using this like as a source of inspiration:
http://stackoverflow.com/questions/6...ct-pressed-key
here's a demo I wrote to check its compatibility with IE and FF:
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function getEnterKey(evt)
  3. {
  4.     var charCode = (evt.which) ? evt.which : evt.keyCode
  5.     if (charCode == 13)
  6.     alert("mine");
  7.     //return true;
  8. }
  9. </script>
  10.  
  11.  
  12.  
  13. <input type="text" onkeypress="return getEnterKey(event);">
  14. <input type="text" onkeydown="return getEnterKey(event);">
  15.  
It works both in IE and FF. In this code, I have used Alert to check whether the function is called or not.

But, in the main example given in the 2nd post of this topic, IE does not work properly. You have to press "Send" button to post data.

I don't know what's wrong. I'm stuck.
Apr 18 '12 #6
Using this golden page again:
http://stackoverflow.com/questions/6...ct-pressed-key

and using your kind advice, I came to a solution to this odd problem. I added this function to the <Head> of my HTML page:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function getEnterKey(winEvent)
  3. {
  4.     var keyCode;
  5.     // event passed as param in Firefox, not IE where it's defined as window.event
  6.     if(!winEvent) {
  7.         winEvent = window.event;
  8.         keyCode = winEvent.keyCode;
  9.     }
  10.     else { // ff
  11.         keyCode = winEvent.which;
  12.     }
  13.  
  14.     if (keyCode == 13)
  15.     {
  16.         sendIt();
  17.         //return true;
  18.     }
  19. }
  20. </script>
The code works in IE and FF now. I need your advice on it. Is it a standard way of handling this? You seem to be an expert in this area.
Apr 18 '12 #7
Dormilich
8,658 Expert Mod 8TB
Is it a standard way of handling this?
when IE is involved, it is not standard any more (that’s more a figure of speech though, but it is well as long as you can make IE do as wanted)
Apr 18 '12 #8
I'm sorry to ask you another question, Dormilich.

How do you quote some part of a post that is written in gray color? I think you have a special BB Code for that.

Thank you for your helpful comments.
Apr 18 '12 #9
Dormilich
8,658 Expert Mod 8TB
indeed, Moderators have a quote-button. *gg*
Apr 19 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: LilAndy23 | last post by:
How can I use the onKeyPress event handler in Firefox? onKeyPress doesn't seem to fire in Firefox.
1
by: ryanmhuc | last post by:
Is there a way to get the caret position of a text input using FireFox. I am aware of ways to such with IE but have been unable to accomplish in FireFox and have not found a solution in the groups...
3
by: Juan | last post by:
Hello: I'm having a problem with a simple javascript code that checks if the enter key had been pressed or not. The code works propertly in mozilla, but in iexplorer it only works one time, the...
4
by: Jake Barnes | last post by:
I wanted to teach myself AJAX this weekend so I sat down with Stuart Landgridge's book and I started to play around. I came up with a little page that you can add text and images to. You can see it...
10
by: Paul Gorodyansky | last post by:
Hi, Ran into the problem today - in INPUT field Firefox executes clean-yp of the content if a user presses Esc, _before_ control goes to the code via onkeydown - and search showed that it's a...
1
by: Simon Wigzell | last post by:
I'm using a javascript function to interecept all key presses and check for valid character and also check the length of the current string and if it is greater than a sent value, set the focus to...
11
by: jimstruckster | last post by:
I have a table with 10 rows, I want all rows except for the first to be hidden when the page first opens up. If the user puts a value in a text box in the first row then I want the second row to...
1
by: Mario Figueiredo | last post by:
Hello everyone, I'm having trouble controlling the cursor position when I make two consecutive calls to the get family of functions. This problem does not happen if there is an output in...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
2
by: Joe Stateson | last post by:
Not sure what is going on. I have a treeview of an XmlDataSource with cacheing enabled. With only a few nodes there is no problem on a postback, the treeview is repainted immediately. With a lot...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.