How to handle input OnKeyPress in FireFox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicebasic
    New Member
    • Aug 2010
    • 91

    How to handle input OnKeyPress in FireFox?

    I have this code:
    Code:
    <input type="text" name="message" id=message size="76">
    &nbsp;
    <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.
  • nicebasic
    New Member
    • Aug 2010
    • 91

    #2
    I found a solution for FireFox, but IE does not work now.

    Inspired by the following page:


    I have changed the code like this:
    Code:
    <script type="text/javascript">
    function getEnterKey(evt)
    {
        var charCode = (evt.which) ? evt.which : evt.keyCode
        if (charCode == 13)
        {
            sendIt();
            //return true;
        }
    }
    </script>
    <input type="text" name="message" id=message size="76" onKeyDown="return getEnterKey(event);">
    &nbsp;
    <input type="submit" value="Send" name="B1" onclick="alert('demo')">
    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.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      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.

      Comment

      • nicebasic
        New Member
        • Aug 2010
        • 91

        #4
        Can you help me change this script to make it compatible with both IE and FF?

        I'll be really grateful to you.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          it depends on how far you want to go back (IE 10 .. 6) to support it. generally, as of IE9 DOM Events are supported.

          Comment

          • nicebasic
            New Member
            • Aug 2010
            • 91

            #6
            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:

            here's a demo I wrote to check its compatibility with IE and FF:
            Code:
            <script>
            function getEnterKey(evt)
            {
                var charCode = (evt.which) ? evt.which : evt.keyCode
                if (charCode == 13)
                alert("mine");
                //return true;
            }
            </script>
            
            
            
            <input type="text" onkeypress="return getEnterKey(event);">
            <input type="text" onkeydown="return getEnterKey(event);">
            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.

            Comment

            • nicebasic
              New Member
              • Aug 2010
              • 91

              #7
              Using this golden page again:


              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:
              Code:
              <script type="text/javascript">
              function getEnterKey(winEvent)
              {
                  var keyCode;
                  // event passed as param in Firefox, not IE where it's defined as window.event
                  if(!winEvent) {
                      winEvent = window.event;
                      keyCode = winEvent.keyCode;
                  }
                  else { // ff
                      keyCode = winEvent.which;
                  }
              
                  if (keyCode == 13)
                  {
                      sendIt();
                      //return true;
                  }
              }
              </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.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                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)

                Comment

                • nicebasic
                  New Member
                  • Aug 2010
                  • 91

                  #9
                  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.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    indeed, Moderators have a quote-button. *gg*

                    Comment

                    Working...