Firefox event.keyCode problem (help......)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rich_poppleton@talk21.com

    Firefox event.keyCode problem (help......)

    Help....

    I've got a textarea where people type in a description. However for
    certain reasons we need to stop them typing !$*^ .

    I have a solution this which works fine in IE:

    function keypress()
    {

    if((event.keyCo de==33)||(event .keyCode==36)|| (event.keyCode= =42)||(event.ke yCode==94))
    {
    //bad key pressed
    event.returnVal ue = false;
    }
    }

    This function is in a javascript file and called from the textarea like
    this:

    <textarea rows="13" maxLength="860" cols="30" name="remarks"
    id="remarks" title="descript ion"
    onchange="keypr ess();" onKeypress="key press();" onKeyDown="keyp ress();"
    onKeyUp="keypre ss();"
    value =""></textarea>

    However, it doesn't work in Firefox. I changed event.keyCode to
    event.which . However, this only works if the javascript is embedded in
    the html and not in my .js file. However, one of the requirements for
    my project is that it is W3c compliant and therefore the javascript
    should be in separate files and not embedded in the html.

    Does anyone know how I can successfully get the event object in a
    javascript file using firefox so I can user event.which ???

    Cheers...Rich

  • Robert

    #2
    Re: Firefox event.keyCode problem (help......)

    rich_poppleton@ talk21.com wrote:[color=blue]
    > Help....
    >
    > I've got a textarea where people type in a description. However for
    > certain reasons we need to stop them typing !$*^ .
    >
    > I have a solution this which works fine in IE:
    >
    > function keypress()
    > {[/color]

    function keypress(event)
    {
    event = event || window.event; // window.event for IE
    ...
    [color=blue]
    > <textarea rows="13" maxLength="860" cols="30" name="remarks"
    > id="remarks" title="descript ion"
    > onchange="keypr ess();" onKeypress="key press();" onKeyDown="keyp ress();"
    > onKeyUp="keypre ss();"
    > value =""></textarea>[/color]

    onchange="keypr ess(event);" onKeypress="key press(event);"
    onKeyDown="keyp ress(event);"

    [color=blue]
    > However, this only works if the javascript is embedded in
    > the html and not in my .js file. However, one of the requirements for
    > my project is that it is W3c compliant and therefore the javascript
    > should be in separate files and not embedded in the html.[/color]

    This part did not make any sense to me.
    [color=blue]
    > Does anyone know how I can successfully get the event object in a
    > javascript file using firefox so I can user event.which ???[/color]

    No need to use event.which. Just use event.keyCode according to W3
    standards.

    Comment

    • Michael Winter

      #3
      Re: Firefox event.keyCode problem (help......)

      On 22/09/2005 14:11, Robert wrote:
      [color=blue]
      > rich_poppleton@ talk21.com wrote:[/color]

      [snip]
      [color=blue][color=green]
      >> function keypress()
      >> {[/color]
      >
      > function keypress(event)
      > {
      > event = event || window.event; // window.event for IE[/color]

      This construct is only necessary when a function object will be assigned
      directly to a event property. That is:

      myElement.onkey press = keypress;

      In a later suggestion (omitted) you pass the event object to the
      function so the above is unnecessary.

      [snip]
      [color=blue][color=green]
      >> However, this only works if the javascript is embedded in
      >> the html and not in my .js file. However, one of the requirements for
      >> my project is that it is W3c compliant and therefore the javascript
      >> should be in separate files and not embedded in the html.[/color][/color]

      Using intrinsic event attributes doesn't violate any particular W3C
      recommendation. If you really want to remove the attributes, then you
      can assign listeners to elements at run-time. However, this addition
      will need to be deferred until the load event is fired.

      var global = this;

      function keypress(e) {
      if((e = e || global.event)) {
      /* Use e to refer to the event object */
      }
      }

      if(document.get ElementById) {
      global.onload = function() {
      var remarks = document.getEle mentById('remar ks');

      if(remarks) {
      remarks.onchang e = remarks.onkeypr ess
      = remarks.onkeydo wn
      = remarks.onkeyup
      = keypress;
      }
      };
      }
      [color=blue][color=green]
      >> Does anyone know how I can successfully get the event object in a
      >> javascript file using firefox so I can user event.which ???[/color]
      >
      > No need to use event.which. Just use event.keyCode according to W3
      > standards.[/color]

      Where did you read that? The only W3C DOM publication that relates to
      keyboard events is contained in DOM 3 Events, which I believe has been
      abandoned as a Working Group Note. Irrespective of its status, it
      contains no mention of a keyCode property.

      The keyCode property is not universal, so testing for it is useful:

      if('number' == typeof e.keyCode) {
      /* Use the keyCode property */
      } else if('number' == typeof e.which) {
      /* Use the which property */
      }

      Mike

      --
      Michael Winter
      Prefix subject with [News] before replying by e-mail.

      Comment

      • Robert

        #4
        Re: Firefox event.keyCode problem (help......)

        Michael Winter wrote:[color=blue]
        > On 22/09/2005 14:11, Robert wrote:[color=green]
        >> function keypress(event)
        >> {
        >> event = event || window.event; // window.event for IE[/color]
        >
        >
        > This construct is only necessary when a function object will be assigned
        > directly to a event property.[/color]

        Yep sorry.
        [color=blue][color=green]
        >> No need to use event.which. Just use event.keyCode according to W3
        >> standards.[/color]
        >
        >
        > Where did you read that?[/color]

        Ohh sigh. It was in the DOM2 Event recommendation, but they removed it :(
        And it seem they will replace it with something else again in DOM3.
        Not very convenient for us.

        keyCode does work fine in IE6 and Firefox.

        Comment

        • Stephen Chalmers

          #5
          Re: Firefox event.keyCode problem (help......)

          <rich_poppleton @talk21.com> wrote in message news:1127390239 .614712.181650@ z14g2000cwz.goo glegroups.com.. .[color=blue]
          > Help....
          >
          > I've got a textarea where people type in a description. However for
          > certain reasons we need to stop them typing !$*^ .
          >
          > I have a solution this which works fine in IE:
          >
          > function keypress()
          > {
          >
          > if((event.keyCo de==33)||(event .keyCode==36)|| (event.keyCode= =42)||(event.ke yCode==94))
          > {
          > file://bad key pressed
          > event.returnVal ue = false;
          > }
          > }
          >
          > This function is in a javascript file and called from the textarea like
          > this:
          >
          > <textarea rows="13" maxLength="860" cols="30" name="remarks"
          > id="remarks" title="descript ion"
          > onchange="keypr ess();" onKeypress="key press();" onKeyDown="keyp ress();"
          > onKeyUp="keypre ss();"
          > value =""></textarea>
          >
          > However, it doesn't work in Firefox. I changed event.keyCode to
          > event.which . However, this only works if the javascript is embedded in
          > the html and not in my .js file. However, one of the requirements for
          > my project is that it is W3c compliant and therefore the javascript
          > should be in separate files and not embedded in the html.
          >
          > Does anyone know how I can successfully get the event object in a
          > javascript file using firefox so I can user event.which ???
          >
          > Cheers...Rich
          >[/color]
          Would this be an acceptable alternative?

          <TEXTAREA onchange='this. value=this.valu e.replace(/[\x24\^\!\*]/g,"");' >

          --
          S.C.


          Comment

          • Gérard Talbot

            #6
            Re: Firefox event.keyCode problem (help......)

            rich_poppleton@ talk21.com a écrit :[color=blue]
            > Help....
            >
            > I've got a textarea where people type in a description. However for
            > certain reasons we need to stop them typing !$*^ .
            >
            > I have a solution this which works fine in IE:
            >
            > function keypress()
            > {
            >
            > if((event.keyCo de==33)||(event .keyCode==36)|| (event.keyCode= =42)||(event.ke yCode==94))
            > {
            > //bad key pressed
            > event.returnVal ue = false;
            > }
            > }
            >
            > This function is in a javascript file and called from the textarea like
            > this:
            >
            > <textarea rows="13" maxLength="860" cols="30" name="remarks"
            > id="remarks" title="descript ion"
            > onchange="keypr ess();" onKeypress="key press();" onKeyDown="keyp ress();"
            > onKeyUp="keypre ss();"
            > value =""></textarea>[/color]

            The above 4 event handlers is unnecessary. If you want to filter 4
            precise printable characters (non-printable ones is another story), then
            you only need to use the keypress event handler.
            [color=blue]
            >
            > However, it doesn't work in Firefox. I changed event.keyCode to
            > event.which . However, this only works if the javascript is embedded in
            > the html and not in my .js file. However, one of the requirements for
            > my project is that it is W3c compliant and therefore the javascript
            > should be in separate files and not embedded in the html.
            >
            > Does anyone know how I can successfully get the event object in a
            > javascript file using firefox so I can user event.which ???
            >
            > Cheers...Rich
            >[/color]

            function init()
            {
            if(document.add EventListener) // DOM 2 Events compliant
            {
            document.getEle mentById("remar ks").addEventLi stener("keypres s",
            Filter4Characte rs, true);
            }
            else if(window.event )
            {
            document.getEle mentById("remar ks").onkeypre ss = Filter4Characte rs;
            };
            }

            function Filter4Characte rs(evt)
            {
            if(evt.charCode && (evt.charCode == 33 || evt.charCode == 36 ||
            evt.charCode == 42 || evt.charCode == 94))
            {
            if(evt.preventD efault)
            {
            evt.preventDefa ult();
            };
            }
            else if(event.keyCod e && (event.keyCode == 33 || event.keyCode == 36 ||
            event.keyCode == 42 || event.keyCode == 94))
            {
            if(event.return Value)
            {
            event.returnVal ue = false;
            };
            };
            }

            ....

            <body onload="init(); " ...>

            Opera 8.5 supports keyCode, is DOM 2 Events compliant and matches IE 6
            event model; so, here, you'll have to test this further.

            Gérard
            --
            remove blah to email me

            Comment

            • Gaston

              #7
              Re: Firefox event.keyCode problem (help......)


              Gérard Talbot ha escrito:
              [color=blue]
              > rich_poppleton@ talk21.com a écrit :[color=green]
              > > Help....
              > >
              > > I've got a textarea where people type in a description. However for
              > > certain reasons we need to stop them typing !$*^ .
              > >
              > > I have a solution this which works fine in IE:
              > >
              > > function keypress()
              > > {
              > >
              > > if((event.keyCo de==33)||(event .keyCode==36)|| (event.keyCode= =42)||(event.ke yCode==94))
              > > {
              > > //bad key pressed
              > > event.returnVal ue = false;
              > > }
              > > }
              > >
              > > This function is in a javascript file and called from the textarea like
              > > this:
              > >
              > > <textarea rows="13" maxLength="860" cols="30" name="remarks"
              > > id="remarks" title="descript ion"
              > > onchange="keypr ess();" onKeypress="key press();" onKeyDown="keyp ress();"
              > > onKeyUp="keypre ss();"[/color][/color]
              [color=blue][color=green]
              > > value =""></textarea>[/color]
              >
              > The above 4 event handlers is unnecessary. If you want to filter 4
              > precise printable characters (non-printable ones is another story), then
              > you only need to use the keypress event handler.
              >[color=green]
              > >
              > > However, it doesn't work in Firefox. I changed event.keyCode to
              > > event.which . However, this only works if the javascript is embedded in
              > > the html and not in my .js file. However, one of the requirements for
              > > my project is that it is W3c compliant and therefore the javascript
              > > should be in separate files and not embedded in the html.
              > >
              > > Does anyone know how I can successfully get the event object in a
              > > javascript file using firefox so I can user event.which ???
              > >
              > > Cheers...Rich
              > >[/color]
              >
              > function init()
              > {
              > if(document.add EventListener) // DOM 2 Events compliant
              > {
              > document.getEle mentById("remar ks").addEventLi stener("keypres s",
              > Filter4Characte rs, true);
              > }
              > else if(window.event )
              > {
              > document.getEle mentById("remar ks").onkeypre ss = Filter4Characte rs;
              > };
              > }
              >
              > function Filter4Characte rs(evt)
              > {
              > if(evt.charCode && (evt.charCode == 33 || evt.charCode == 36 ||
              > evt.charCode == 42 || evt.charCode == 94))
              > {
              > if(evt.preventD efault)
              > {
              > evt.preventDefa ult();
              > };
              > }
              > else if(event.keyCod e && (event.keyCode == 33 || event.keyCode ==36 ||
              > event.keyCode == 42 || event.keyCode == 94))
              > {
              > if(event.return Value)
              > {
              > event.returnVal ue = false;
              > };
              > };
              > }
              >
              > ...
              >
              > <body onload="init(); " ...>
              >
              > Opera 8.5 supports keyCode, is DOM 2 Events compliant and matches IE 6
              > event model; so, here, you'll have to test this further.
              >
              > Gérard
              > --
              > remove blah to email me[/color]


              CHECKING KEYPRESS FIREFOX - ONLY NUMBERS

              Hola no se si esto te servira

              en mi HTML, JPG, etc

              <INPUT TYPE="TEXT" onKeyPress="ret urn checkNumero(eve nt)">


              luego en mi javascript

              function CheckNumero(eve nt)
              {
              var key=event.which ;//codigo de tecla.

              if (key==8) return true;

              if (key < 48 || key > 57)
              { //si no es numero
              return false
              }
              return true
              }


              Entonces con solo devolver false al input la key apretada no se
              marcara. Solo con return true

              Comment

              Working...