Enter Key not firing in Firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yellowdog
    New Member
    • May 2006
    • 1

    Enter Key not firing in Firefox

    Hi.

    I have an .aspx page that I'm trying to get to fire some Javascript on an Enter Key press inside a text box in a Firefox browser.

    Here is the .aspx/html code:

    <input type="text" runat="server" id="tbSearch" onkeypress="jav ascript:SearchE nter();">

    And here is the java it's trying to fire:

    function SearchEnter()
    {

    if ( (window.event && window.event.ke yCode == 13) || (window.event.c harCode == 13))
    {
    alert("SearchEn ter");
    SubmitSearch();
    }
    }

    The alert message never appears.

    Of course this works fine in IE, just not Firefox.

    Any help would be greatly appreciated.

    yd
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    window.event (the event member of the window object) does not exist in Firefox (or any Mozilla implementation) .

    Unfortunately while a lot of things to do with web standards and programming have been standardised events are not one of them .

    If order to capture both the Mozilla and IE event objects I think you need to change your SearchEnter function like so

    Code:
    function SerachEnter(e)
    {
    	if (!e) var e = window.event
    	// e refers to the event
    
    	/* Rest of code here */
    }
    you may also need to change your html to

    [html]
    <input type="text" runat="server" id="tbSearch" onkeypress="Sea rchEnter(event) ;">
    [/html]

    Comment

    • syedfasih
      New Member
      • Feb 2007
      • 10

      #3
      use e.which rathre than e.keyCode this will solve your problem :)

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        if window.event does not support use window.evt

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Syed is right, for browsers which do not support keyCode, use e.which.

          For information on events and browser compatibility, see the following links:

          Comment

          Working...