Strange onkeydown behaviour (works with alert, NOT without)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • F. Da Costa

    Strange onkeydown behaviour (works with alert, NOT without)

    Hi,

    This is a strange issue I'v been staring at for half a day now.
    It concerns catching keys via the onkeydown handler.

    In IE5+ it works fine but in Moz 1.6 (& Firebird 0.7+) it behaves most
    peculiar. The 'offensive' code is included below.

    The weird thing lies in the alert box preceded by // **.
    * function textboxReplaceS elect *
    If the alert is active it works perfect. No key duplication in the textbox
    on the screen.
    insert the comment ()i.e. take out the alert) and one gets two characters
    instead of one.
    It almost looks like something 'shoots through' because (to make things
    worse) the last alert shows me a singular entry as well.

    To test the code just use the following

    TIA
    Fermin DCG

    <html>
    <head>
    <script>
    var isIE = navigator.userA gent.indexOf('M SIE') > 1;
    var isMoz = navigator.userA gent.indexOf('M ozilla/5.') == 0;var _character;
    var _keyCode;
    var _charCode;

    // decipher key down codes
    function showDown(oTextb ox, evt) {
    _keyCode = _charCode = _character = null;
    evt = (evt) ? evt : ((event) ? event : null);

    if (evt) {
    _keyCode = evt.keyCode;
    if (evt.charCode) _charCode = evt.charCode;
    else _charCode = (isIE) ? _keyCode : ((_charCode) ? _charCode : _keyCode);
    _character = String.fromChar Code(_charCode) .toLowerCase();
    }
    textboxReplaceS elect(oTextbox, _character);
    return false;
    }


    /** THE OFFENSIVE CODE
    * Replace the currently selected text with some other text
    * F.e.: when a <x> is pressed in an empty textbox it should come out with
    1 x only on the screen.
    * @param oTextbox = the textbox to act on
    * @param sText = the text to insert
    */
    function textboxReplaceS elect (oTextbox, sText) {
    if (isIE) {
    var oRange = document["selection"].createRange();
    oRange.text = sText;
    oRange.collapse (true);
    oRange.select() ;
    } else if (isMoz) { // THIS DOES NOT WORK AS EXPECTED
    var oldText = oTextbox["value"];
    var iSelEnd = oTextbox["selectionE nd"];
    var iStart = oTextbox["selectionStart "];
    oTextbox.value = "";
    // ** UNCOMMENT AND THE CODE WORKS
    //alert("1.textbo xReplaceSelect: :"+oldText+" - "+sText+" - "+
    oTextbox.value) ;
    //oTextbox.value = oTextbox.value. substring(0, iStart) + sText +
    oTextbox.value. substring(oText box.selectionEn d, oTextbox.value. length);
    oTextbox.value = oldText.substri ng(0, iStart) + sText +
    oldText.substri ng(iSelEnd, oldText.length) ;
    //alert("2.textbo xReplaceSelect: :"+oTextbox.val ue+" - "+sText);
    oTextbox.setSel ectionRange(iSt art + sText.length, iStart + sText.length);
    }

    oTextbox.focus( );
    alert("exit.tex tboxReplaceSele ct::"+oTextbox. value+" - "+sText);
    }

    </script>
    </head>
    <body>
    <input type="text" value="" id="txtSearch" onkeydown="retu rn showDown(this,
    event);" />
    </body>
    </html>
  • Janwillem Borleffs

    #2
    Re: Strange onkeydown behaviour (works with alert, NOT without)


    "F. Da Costa" <dacosta@xs4all .nl> schreef in bericht
    news:3fe05040$0 $238$e4fe514c@n ews.xs4all.nl.. .[color=blue]
    >
    > var isIE = navigator.userA gent.indexOf('M SIE') > 1;
    > var isMoz = navigator.userA gent.indexOf('M ozilla/5.') == 0;[/color]

    Don't use the userAgent string for browser detection, but test for available
    objects/functions instead:

    var isIE = document.all;
    var isMoz = document.getEle mentById && !document.all;
    [color=blue]
    > oTextbox.setSel ectionRange(iSt art + sText.length, iStart +[/color]
    sText.length);

    This should be:
    oTextbox.setSel ectionRange(iSt art, iStart + sText.length);


    JW



    Comment

    • HikksNotAtHome

      #3
      Re: Strange onkeydown behaviour (works with alert, NOT without)

      In article <3fe0841a$0$193 $1b62eedf@news. wanadoo.nl>, "Janwillem Borleffs"
      <jw@jwscripts.c om> writes:
      [color=blue]
      >var isIE = document.all;[/color]

      IE is not the only browser that can pass that test. Opera can pass it (in IE
      spoof mode), as well as one that Jim Ley mentioned a while back (can't remember
      the name of it), that supports both document.all and document.layers
      [color=blue]
      >var isMoz = document.getEle mentById && !document.all;[/color]

      Opera can pass that test.

      var mightBeMoz = document.getEle mentById && !document.all && !window.opera;

      But its still better than attempted browser detection :)

      var useGEBI = document.getEle mentById;
      var useAll = document.all;
      var useLayers = document.layers ;

      if (useGEBI){

      }
      else{
      if (useAll){

      }
      else{
      if (useLayers){

      }else{
      alert('You cant do that!')
      }
      }
      }

      I might have mismatched { } in there, but the idea is there :)

      Ain't object detection grand?
      --
      Randy

      Comment

      • Janwillem Borleffs

        #4
        Re: Strange onkeydown behaviour (works with alert, NOT without)


        "HikksNotAtHome " <hikksnotathome @aol.com> schreef in bericht
        news:2003121712 3100.23171.0000 2738@mb-m05.aol.com...[color=blue]
        > In article <3fe0841a$0$193 $1b62eedf@news. wanadoo.nl>, "Janwillem Borleffs"
        > <jw@jwscripts.c om> writes:
        >[color=green]
        > >var isIE = document.all;[/color]
        >
        > IE is not the only browser that can pass that test. Opera can pass it (in[/color]
        IE[color=blue]
        > spoof mode), as well as one that Jim Ley mentioned a while back (can't[/color]
        remember[color=blue]
        > the name of it), that supports both document.all and document.layers
        >[/color]

        You are right, but I believe that document.all represents a function rather
        then a property, so:

        var isOpera = document.all && 'function' == typeof document.all;

        would evaluate for Opera without using the window.opera property.


        JW



        Comment

        • Richard Cornford

          #5
          Re: Strange onkeydown behaviour (works with alert, NOT without)

          "HikksNotAtHome " <hikksnotathome @aol.com> wrote in message
          news:2003121712 3100.23171.0000 2738@mb-m05.aol.com...
          <snip>[color=blue][color=green]
          > >var isIE = document.all;[/color]
          >
          >IE is not the only browser that can pass that test. Opera can
          >pass it (in IE spoof mode), as well as one that Jim Ley mentioned
          >a while back (can't remember the name of it), that supports both
          >document.all and document.layers[/color]

          Along with Konqueror (so probably Safari), IceBrowser, Web Browser 2.0
          and so on, indeed there are probably more browsers these days that
          support document.all than don't.

          <snip>[color=blue]
          > But its still better than attempted browser detection :)
          >
          > var useGEBI = document.getEle mentById;
          > var useAll = document.all;
          > var useLayers = document.layers ;
          >
          > if (useGEBI){
          >
          > }
          > else{
          > if (useAll){[/color]
          <snip>[color=blue]
          > Ain't object detection grand?[/color]

          Fair enough, but the OPs decision making is related to, for example,
          document.select ion so the specific replacement for the next to useless -
          isIE - test may be more along the lines of:-

          var useDocSelection = ((document.sele ction)&&
          (document.selec tion.createRang e));

          with additional test to verify that any created Range object has the
          required - collapse - and - select - method prior to attempting to use
          them. But the "Mozilla" branch would need to make a more direct test of
          the features of the - oTextbox - object to see if it supported the
          features required by that branch. Certainly support for -
          document.getele mentById - in the absence of - document.all - says
          nothing about the ability to call - setSelectedRang e - on the -
          Textbox - object.

          Richard.


          Comment

          • F. Da Costa

            #6
            Re: Strange onkeydown behaviour (works with alert, NOT without)

            Janwillem Borleffs wrote:[color=blue]
            > "F. Da Costa" <dacosta@xs4all .nl> schreef in bericht
            > news:3fe05040$0 $238$e4fe514c@n ews.xs4all.nl.. .
            >[color=green]
            >>var isIE = navigator.userA gent.indexOf('M SIE') > 1;
            >>var isMoz = navigator.userA gent.indexOf('M ozilla/5.') == 0;[/color]
            >
            >
            > Don't use the userAgent string for browser detection, but test for available
            > objects/functions instead:
            >
            > var isIE = document.all;
            > var isMoz = document.getEle mentById && !document.all;[/color]
            Point taken, but it does not influence the issue as described in any way.[color=blue]
            >
            >[color=green]
            >> oTextbox.setSel ectionRange(iSt art + sText.length, iStart +[/color]
            >
            > sText.length);
            >
            > This should be:
            > oTextbox.setSel ectionRange(iSt art, iStart + sText.length);[/color]
            Noop, because the first part of th string should *not* be selected.
            However, this also does *not* cover the issue at hand.[color=blue]
            >
            >
            > JW[/color]
            Thx anyway,
            F DCG

            Comment

            • F. Da Costa

              #7
              Re: Strange onkeydown behaviour **rephrased**

              F. Da Costa wrote:[color=blue]
              > Hi,
              >
              > This is a strange issue I'v been staring at for half a day now.
              > It concerns catching keys via the onkeydown handler.
              >
              > In IE5+ it works fine but in Moz 1.6 (& Firebird 0.7+) it behaves most
              > peculiar. The 'offensive' code is included below.
              >
              > The weird thing lies in the alert box preceded by // **.
              > * function textboxReplaceS elect *
              > If the alert is active it works perfect. No key duplication in the textbox
              > on the screen.
              > insert the comment ()i.e. take out the alert) and one gets two
              > characters instead of one.
              > It almost looks like something 'shoots through' because (to make things
              > worse) the last alert shows me a singular entry as well.
              >
              > To test the code just use the following
              >
              > TIA
              > Fermin DCG
              >[/color]
              <html>
              <head>
              <script>
              var isIE = navigator.userA gent.indexOf('M SIE') > 1;
              var isMoz = navigator.userA gent.indexOf('M ozilla/5.') == 0;var _character;
              var _keyCode;
              var _charCode;

              // decipher key down codes
              function showDown(oTextb ox, evt) {
              _keyCode = _charCode = _character = null;
              evt = (evt) ? evt : ((event) ? event : null);

              if (evt) {
              _keyCode = evt.keyCode;
              if (evt.charCode) _charCode = evt.charCode;
              else _charCode = (isIE) ? _keyCode : ((_charCode) ? _charCode : _keyCode);
              _character = String.fromChar Code(_charCode) .toLowerCase();
              }
              textboxReplaceS elect(oTextbox, _character);
              return false;
              }


              /** THE OFFENSIVE CODE
              * Replace the currently selected text with some other text
              * F.e.: when a <x> is pressed in an empty textbox it should come out with
              1 x only on the screen.
              * @param oTextbox = the textbox to act on
              * @param sText = the text to insert
              */
              function textboxReplaceS elect (oTextbox, sText) {
              if (isIE) {
              var oRange = document["selection"].createRange();
              oRange.text = sText;
              oRange.collapse (true);
              oRange.select() ;
              } else if (isMoz) {
              var oldText = oTextbox["value"];
              var iSelEnd = oTextbox["selectionE nd"];
              var iStart = oTextbox["selectionStart "];
              oTextbox.value = "";
              // === THE FOLLOWING DOES NOT WORK AS EXPECTED ===
              // ** UNCOMMENT THE ALERT AND THE CODE WORKS WITHOUT IT, IT DOESN'T
              //alert("1.textbo xReplaceSelect: :"+oldText+" - "+sText+" - "+
              oTextbox.value) ;
              //oTextbox.value = oTextbox.value. substring(0, iStart) + sText +
              oTextbox.value. substring(oText box.selectionEn d, oTextbox.value. length);
              oTextbox.value = oldText.substri ng(0, iStart) + sText +
              oldText.substri ng(iSelEnd, oldText.length) ;
              //alert("2.textbo xReplaceSelect: :"+oTextbox.val ue+" - "+sText);
              oTextbox.setSel ectionRange(iSt art + sText.length, iStart + sText.length);
              }

              oTextbox.focus( );
              alert("exit.tex tboxReplaceSele ct::"+oTextbox. value+" - "+sText);
              }

              </script>
              </head>
              <body>
              <input type="text" value="" id="txtSearch" onkeydown="retu rn showDown(this,
              event);" />
              </body>
              </html>

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Strange onkeydown behaviour (works with alert, NOT without)

                Janwillem Borleffs wrote:
                [color=blue]
                > "HikksNotAtHome " <hikksnotathome @aol.com> schreef in bericht
                > news:2003121712 3100.23171.0000 2738@mb-m05.aol.com...[/color]

                It is called attribution _line_, not attribution novel.
                Those who know what a message ID is, do know where to
                find it.
                [color=blue][color=green]
                >> [...] "Janwillem Borleffs" [...] writes:[color=darkred]
                >>> var isIE = document.all;[/color]
                >>
                >> IE is not the only browser that can pass that test. Opera can pass
                >> it (in IE spoof mode), as well as one that Jim Ley mentioned a
                >> while back (can't remember the name of it), that supports both
                >> document.all and document.layers[/color]
                >
                > You are right, but I believe that document.all represents a function
                > rather then a property, so:[/color]

                Functions/methods are properties of type `function' (or `object').
                [color=blue]
                > var isOpera = document.all && 'function' == typeof document.all;[/color]

                I prefer to use the constant value to be compared with right-hand side.
                [color=blue]
                > would evaluate for Opera without using the window.opera property.[/color]

                It would not. Instead, it would falsely allow all UAs who support that
                part of the DOM of the IE 4+ browser component to be handled as if Opera
                would be used.


                PointedEars

                P.S.:
                Your UA (Outlook Express) is borken which makes your postings hardly
                leagible. Please use OE Quotefix, a similar tool or simply a working UA.

                Comment

                • Janwillem Borleffs

                  #9
                  Re: Strange onkeydown behaviour (works with alert, NOT without)


                  "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> schreef in bericht
                  news:3FECD9E8.1 060602@PointedE ars.de...[color=blue]
                  >
                  > Your UA (Outlook Express) is borken which makes your postings hardly
                  > leagible. Please use OE Quotefix, a similar tool or simply a working UA.[/color]

                  What is broken about my UA??????????


                  Comment

                  • Janwillem Borleffs

                    #10
                    Re: Strange onkeydown behaviour (works with alert, NOT without)

                    Janwillem Borleffs wrote:[color=blue]
                    > "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> schreef in bericht
                    > news:3FECD9E8.1 060602@PointedE ars.de...[color=green]
                    >>
                    >> Your UA (Outlook Express) is borken which makes your postings hardly
                    >> leagible. Please use OE Quotefix, a similar tool or simply a working
                    >> UA.[/color]
                    >
                    > What is broken about my UA??????????[/color]

                    Is this better?


                    JW



                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: Strange onkeydown behaviour (works with alert, NOT without)

                      Janwillem Borleffs wrote:
                      [color=blue]
                      > Janwillem Borleffs wrote:[color=green]
                      >> "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> schreef in bericht
                      >> news:3FECD9E8.1 060602@PointedE ars.de...[color=darkred]
                      >>>
                      >>> Your UA (Outlook Express) is borken which makes your postings hardly
                      >>> leagible. Please use OE Quotefix, a similar tool or simply a working[/color][/color][/color]
                      ^^
                      Sorry, my bad. s/ea/e/
                      [color=blue][color=green][color=darkred]
                      >>> UA.[/color]
                      >>
                      >> [...][/color]
                      >
                      > Is this better?[/color]

                      Yes, it is.


                      F'up2 poster

                      PointedEars

                      Comment

                      • Dr John Stockton

                        #12
                        Re: Strange onkeydown behaviour (works with alert, NOT without)

                        JRS: In article <3fed8ca5$0$131 $3b62cedd@news. wanadoo.nl>, seen in
                        news:comp.lang. javascript, Janwillem Borleffs <jw@jwscripts.c om> posted
                        at Sat, 27 Dec 2003 14:44:03 :-[color=blue]
                        >
                        >"Thomas 'PointedEars' Lahn" <PointedEars@we b.de> schreef in bericht
                        >news:3FECD9E8. 1060602@Pointed Ears.de...[color=green]
                        >>
                        >> Your UA (Outlook Express) is borken which makes your postings hardly
                        >> leagible. Please use OE Quotefix, a similar tool or simply a working UA.[/color]
                        >
                        >What is broken about my UA??????????[/color]

                        Ignore him; he wants to be a dictator. The concept is discredited.

                        --
                        © John Stockton, Surrey, UK. ???@merlyn.demo n.co.uk Turnpike v4.00 MIME. ©
                        Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
                        Check boilerplate spelling -- error is a public sign of incompetence.
                        Never fully trust an article from a poster who gives no full real name.

                        Comment

                        Working...