SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    window.event is not working in Firefox

    The following code is working in IE, but not in firefox :

    <script>
    var prevIndex = -1;

    function SearchName(txtObj,selObj)
    {
    var keyPressed = window.event.keyCode;

    var pressedKey = String.fromCharCode(keyPressed);
    var nm = txtObj.value;
    var count = selObj.length;
    var i;
    var txtLength;
    var txtValue;

    if(keyPressed == 13)
    {
    prevIndex = -1;
    return false;
    }
    nm += pressedKey;
    var listItems = selObj;
    if (prevIndex != -1)
    {
    listItems.item(prevIndex).selected=false;
    }
    if (isNaN(nm))
    {
    for (i=0;i<count;i++)
    {
    txtLength = nm.length;
    txtValue = listItems.item(i).text;
    if (txtValue.substr(0,txtLength).toUpperCase() == nm.toUpperCase() )
    {
    listItems.item(i).selected=true;
    prevIndex = i;
    break;
    }
    }
    }
    else
    {
    for (i=0;i<count;i++)
    {
    txtLength = nm.length;
    txtValue = listItems.item(i).text;
    if (txtValue.indexOf(nm) > 0)
    {
    listItems.item(i).selected=true;
    prevIndex = i;
    break;
    }
    }
    }
    }

    </script>





    <input type="text" name="txt1" onKeyPress="SearchName(this,selList)" value="<Type To Search>" onClick="javascript:this.value='';" style="width:200px;">

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because Firefox handles events slightly different. Instead of using window.event, you should add a parameter to the function signature, like so:
    Code:
    function SearchName(e, txtObj, selObj) {
      evt = e || window.event;
      var keyPressed = evt.which || evt.keyCode;
    }
    and then make your function call like so:
    Code:
    <input type="text" name="txt1" onKeyPress="SearchName(event,this,selList)" value="<Type To Search>" onClick="javascript:this.value='';" style="width:200px;">

  3. #3
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Thanks

    I changed the code as follows :

    * function SearchName(txtObj,selObj,e)

    * var keyPressed;

    if(window.event)
    keyPressed = window.event.keyCode; // IE
    else
    keyPressed = e.which; // Firefox

    * <input type="text" name="txt1" onKeyPress="SearchName(this,selList,event)" value="<Type To Search>" onClick="javascript:this.value='';" style="width:200px;">


    Now, it's fine in IE and FF.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,593
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    if(window.event)
    keyPressed = window.event.keyCode; // IE hack
    else
    keyPressed = e.which; // standard method

    The else code isn't Firefox specific, it is the STANDARD.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    Question window.event.returnValue = false in firefox

    It works in IE but doesn't work in Firefox.

    I have found that FF (and NN) doesn't understud:

    window.event.returnValue = false;

    Pls let me know the equivalent code in FF

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    e.preventDefault();
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Thank u. I got it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •