fireEvent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • znndrp

    fireEvent

    Hi all,

    Anyone knows how to use fireEvent to simulate a shit+keypress in a textarea?

    thanks,

    --
    ,_,
    (O,O)
    ( )
    ---"-"-alex
  • Michael Winter

    #2
    Re: fireEvent

    On Tue, 17 Aug 2004 10:35:36 +0200, znndrp <erased@xs4all. no.spam> wrote:
    [color=blue]
    > Anyone knows how to use fireEvent to simulate a shit+keypress in a
    > textarea?[/color]

    There is no standard approach. The DOM 2 Events Specification defined ways
    of dispatching HTML, mouse, and UI events into the document tree, but
    keyboard events were not specified. The DOM 3 Events Specification does
    add keyboard events, but the Specification was never agreed upon, so
    few[1] browsers implemented it.

    That means that anything you might do, *might* work in Mozilla and IE, but
    it's highly unlikely to work anywhere else.

    Sorry,
    Mike


    [1] Mozilla did, but their implementation was made early, and properties
    and methods have changed since.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail

    Comment

    • Yann-Erwan Perio

      #3
      Re: fireEvent

      znndrp wrote:
      [color=blue]
      > Anyone knows how to use fireEvent to simulate a shit+keypress in a
      > textarea?[/color]

      You seem to be targeting IE only, so the solution will be IE only. Note
      that triggering a keypress event does not deal with the built-in
      pre-event mechanics, i.e. you won't see any letter in your textarea. As
      for manipulating the textarea's value, use IE ranges.

      Though the idea of triggering events is a good one, I believe that there
      usually are better approaches, using handlers and "proxies", but not
      knowing what you want to do makes it difficult to determine the best one:-)


      ---
      <form>
      <textarea onkeypress="foo (this, event)"></textarea>
      <input type="button" value="bar()" onclick="bar()" >
      </form>

      <script type="text/javascript">
      function foo(elem, evt){
      alert(
      [
      "elem : " + elem.nodeName,
      "shiftKey : " + evt.shiftKey,
      "keyCode : " + evt.keyCode
      ].join("\n")
      );
      }

      function bar(){
      if(document.cre ateEventObject) {
      var evt=document.cr eateEventObject ();
      evt.shiftKey=tr ue;
      evt.keyCode=121 ;
      document.forms[0].elements[0].fireEvent("onk eypress", evt);
      }
      }
      </script>
      ---


      HTH,
      Yep.

      Comment

      • znndrp

        #4
        Re: fireEvent

        Yann-Erwan Perio wrote:
        [color=blue]
        > znndrp wrote:
        >[color=green]
        >> Anyone knows how to use fireEvent to simulate a shit+keypress in a
        >> textarea?[/color]
        >
        >
        > You seem to be targeting IE only, so the solution will be IE only. Note
        > that triggering a keypress event does not deal with the built-in
        > pre-event mechanics, i.e. you won't see any letter in your textarea. As
        > for manipulating the textarea's value, use IE ranges.
        >
        > Though the idea of triggering events is a good one, I believe that there
        > usually are better approaches, using handlers and "proxies", but not
        > knowing what you want to do makes it difficult to determine the best one:-)[/color]

        I think I don't know myself either :) But I don't think this is the solution
        to my problem. While I was reading some more on this subject I also noticed
        there's method called 'handleEvent()' . What's the difference between
        fireEvent and handleEvent?

        --
        ,_,
        (O,O)
        ( )
        ---"-"-alex

        Comment

        • Martin Honnen

          #5
          Re: fireEvent



          znndrp wrote:

          [color=blue]
          > Anyone knows how to use fireEvent to simulate a shit+keypress in a
          > textarea?[/color]

          fireEvent (as implemented in IE 5.5 and 6) only allows you to have any
          event handler for the event being fired but for key events for instance
          a key is never entered that way.

          --

          Martin Honnen


          Comment

          • Yann-Erwan Perio

            #6
            Re: fireEvent

            znndrp wrote:
            [color=blue]
            > I think I don't know myself either :) But I don't think this is the
            > solution to my problem.[/color]

            What about telling us the test scenario? This might help!
            [color=blue]
            > While I was reading some more on this subject I
            > also noticed there's method called 'handleEvent()' . What's the
            > difference between fireEvent and handleEvent?[/color]

            There are 2 main events models in the web-browsers' world: Internet
            Explorer model, and W3C (standard) model. Both generally permit to do
            the same things (IE's model being much limited) but are structured and
            scripted differently.

            fireEvent and handleEvent are not part of the same model, and moreover
            don't do the same thing:-)

            fireEvent is part of the IE's events model; it is used to dispatch an
            event from a specific target, with an optional event "template". The W3C
            equivalent is dispatchEvent.

            <URL:http://msdn.microsoft. com/workshop/author/dhtml/reference/methods/fireevent.asp>

            handleEvent is part of the W3C's model and has no equivalent in IE. It
            is the only method of the EventListener interface, i.e. this is the
            handler itself. Usually, we define event listeners this way:

            document.onclic k=function(evt) {alert("hello") ;};

            This is a cross-browser way; using W3C's model you can do:

            document.addEve ntListener(
            "click",
            function(evt){a lert("hello");} ,
            false //no capture
            );

            or (and this is where handleEvent comes out)

            document.addEve ntListener(
            "click",
            {handleEvent:fu nction(evt){ale rt("hello");}} ,
            false
            );

            The latest isn't well-supported though, the preferred way is to pass
            directly the handler as second argument.

            <URL:http://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#Eve nts-EventListener-handleEvent>

            The equivalent IE way (to some extent) would be:

            document.attach Event(
            "onclick",
            function(){aler t("hello");}
            );


            HTH
            Yep.

            Comment

            • Michael Winter

              #7
              Re: fireEvent

              On Tue, 17 Aug 2004 12:04:37 +0200, znndrp <erased@xs4all. no.spam> wrote:

              [snip]
              [color=blue]
              > What's the difference between fireEvent and handleEvent?[/color]

              You're getting confused between two different DOMs.

              fireEvent() is part of Microsoft's proprietary event model. It dispatches
              a user-create event into the document tree.

              handleEvent() is part of the World Wide Web Consortium's (W3C) DOM 2
              Events specification. However, this method doesn't exist for JavaScript
              (look at the ECMAScript bindings for the EventListener interface). In
              JavaScript, an EventListener is a function, so handleEvent() isn't
              necessary. In Java, EventListener is an interface which cannot do anything
              in itself. The programmer must implement the handleEvent() method to
              define behaviour for that listener[1]. In other words, handleEvent() is
              called in Java in the same way a handler function is in JavaScript.

              The equivalent in the W3C DOM is dispatchEvent() .

              Hope that helps,
              Mike


              [1] Sorry, that's as good an explanation as I can come up with at the
              moment. If you know the Java language, you'll know what an interface is,
              and I shouldn't need to it describe how to use the mechanism.

              --
              Michael Winter
              Replace ".invalid" with ".uk" to reply by e-mail

              Comment

              • Michael Winter

                #8
                Re: fireEvent

                On Tue, 17 Aug 2004 12:36:57 +0200, Yann-Erwan Perio
                <y-e.perio@em-lyon.com> wrote:

                [snip]
                [color=blue]
                > or (and this is where handleEvent comes out)
                >
                > document.addEve ntListener(
                > "click",
                > {handleEvent:fu nction(evt){ale rt("hello");}} ,
                > false
                > );
                >
                > The latest isn't well-supported though, the preferred way is to pass
                > directly the handler as second argument.[/color]

                Are you certain about that? The handleEvent() method is described in both
                level 2 and 3, but it isn't in the ECMAScript bindings for either. In
                fact, it states in level 2:

                Object EventListener
                This is an ECMAScript function reference. This method has no
                return value. The parameter is a Event object.

                and more directly in level 3:

                EventListener function:
                This function has no return value. The parameter is an object
                that implements the Event interface.

                [snip]

                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail

                Comment

                • znndrp

                  #9
                  Re: fireEvent

                  Yann-Erwan Perio wrote:
                  [color=blue]
                  > znndrp wrote:
                  >[color=green]
                  >> I think I don't know myself either :) But I don't think this is the
                  >> solution to my problem.[/color]
                  >
                  >
                  > What about telling us the test scenario? This might help![/color]

                  Sorry :), I've got this htmledit object which treates a single [enter] as
                  '</p><p>' and a [shift]+[enter] as '<br>'. My goal is to make this work the
                  other way round (there's been a few threads on their forum so I doubt it's
                  even possible :/). So I thought 'let's capture those events cancel them, and
                  fire a new event with the 'shiftKey' property inversed'. But that doesn't work.

                  The object im using can be found here:
                  htmlarea.com is your first and best source for information about htmlarea. Here you will also find topics relating to issues of general interest. We hope you find what you are looking for!


                  The function I'm hacking is the 'editor_event() ' function in 'editor.js'

                  --
                  ,_,
                  (O,O)
                  ( )
                  ---"-"-alex

                  Comment

                  • Richard Cornford

                    #10
                    Re: fireEvent

                    Michael Winter wrote:[color=blue]
                    > Yann-Erwan Perio wrote:
                    > [snip][color=green]
                    >> or (and this is where handleEvent comes out)
                    >>
                    >> document.addEve ntListener(
                    >> "click",
                    >> {handleEvent:fu nction(evt){ale rt("hello");}} ,
                    >> false
                    >> );
                    >>
                    >> The latest isn't well-supported though, the preferred way is to pass
                    >> directly the handler as second argument.[/color]
                    >
                    > Are you certain about that? The handleEvent() method is described in
                    > both level 2 and 3, but it isn't in the ECMAScript bindings for
                    > either. In fact, it states in level 2:[/color]

                    Mozilla browsers allow (or have allowed) a Java-style object with a -
                    handleEvent - method to be used in place of a function reference. But as
                    the behaviour is contradictory with the ECMAScript binding for DOM
                    events it probably isn't a good idea to ever use it. Indeed, accompanied
                    by a failure to properly distinguish between Java and javascript, it has
                    been the cause of 'cross-browser problems' reported to the group at
                    least once.
                    [color=blue]
                    > Object EventListener
                    > This is an ECMAScript function reference. This method has no
                    > return value. The parameter is a Event object.[/color]
                    <snip>

                    Yes, in ECMAScript the function object *is* the - EventListener -
                    interface.

                    Richard.


                    Comment

                    • Martin Honnen

                      #11
                      Re: fireEvent



                      Michael Winter wrote:
                      [color=blue]
                      > Yann-Erwan Perio wrote:
                      >[color=green]
                      >> or (and this is where handleEvent comes out)
                      >>
                      >> document.addEve ntListener(
                      >> "click",
                      >> {handleEvent:fu nction(evt){ale rt("hello");}} ,
                      >> false
                      >> );
                      >>
                      >> The latest isn't well-supported though, the preferred way is to pass
                      >> directly the handler as second argument.[/color]
                      >
                      >
                      > Are you certain about that? The handleEvent() method is described in
                      > both level 2 and 3, but it isn't in the ECMAScript bindings for either.
                      > In fact, it states in level 2:
                      >
                      > Object EventListener
                      > This is an ECMAScript function reference. This method has no
                      > return value. The parameter is a Event object.
                      >
                      > and more directly in level 3:
                      >
                      > EventListener function:
                      > This function has no return value. The parameter is an object
                      > that implements the Event interface.[/color]

                      I think you are right that the ECMAScript binding doesn't in any way
                      suggest that an object with a handleEvent method can be used as an event
                      listener but Mozilla has always supported that e.g.

                      document.addEve ntListener(
                      'click',
                      { handleEvent: function (evt) {
                      document.body.a ppendChild(docu ment.createText Node(evt.type + ' '));
                      }
                      },
                      false
                      )

                      besides taking a function object directly. As Opera 7 however will throw
                      an error with the above construct when it tries to call the event
                      listener it is not recommended to use it when scripting HTML documents.

                      But there are other DOM Events implementation, for instance in SVG
                      documents, and with Batik 1.5.1, a Java based SVG viewer that allows
                      ECMAScript to script the SVG document it is possible to use the above
                      construct e.g. in

                      <?xml version="1.0" encoding="UTF-8"?>
                      <svg
                      xmlns="http://www.w3.org/2000/svg"
                      viewBox="0 0 100 100"
                      onload="init(ev t);">

                      <script type="text/ecmascript"><![CDATA[
                      var svgDocument;

                      var svgNamespace = 'http://www.w3.org/2000/svg';

                      function init (evt) {
                      svgDocument = evt.target.owne rDocument;
                      if (svgDocument) {
                      svgDocument.add EventListener(
                      'click',
                      { handleEvent: function (evt) {
                      var tspan = svgDocument.cre ateElementNS(sv gNamespace, 'tspan');
                      tspan.appendChi ld(svgDocument. createTextNode( evt.type + ' '));
                      svgDocument.get ElementById('de bug').appendChi ld(tspan);
                      }
                      },
                      false
                      );
                      }
                      }
                      ]]></script>
                      <rect x="0" y="0" width="100" height="100" fill="lightblue " />

                      <text id="debug" x="0" y="10" font-size="3"></text>

                      </svg>

                      the handleEvent method is called when someone clicks.

                      --

                      Martin Honnen


                      Comment

                      • Yann-Erwan Perio

                        #12
                        Re: fireEvent

                        Michael Winter wrote:
                        [color=blue]
                        > Are you certain about that? The handleEvent() method is described in
                        > both level 2 and 3, but it isn't in the ECMAScript bindings for either.[/color]

                        Honestly I hadn't looked at the EcmaScript bindings, so I'm definitely
                        not certain:-) While in the process of learning events, I had tested
                        properties/methods directly from the specs, and inferred possible usages
                        from having them work on my browsers. handleEvent working in Mozilla was
                        therefore enough for me.
                        [color=blue]
                        > EventListener function:
                        > This function has no return value. The parameter is an object
                        > that implements the Event interface.[/color]

                        Ah that's true, but I also read :

                        addEventListene r(type, listener, useCapture)
                        This function has no return value.
                        The type parameter is a String.
                        The listener parameter is an object that implements
                        the EventListener interface.
                        The useCapture parameter is a Boolean.

                        in which case the use for {handlerEvent:f unc} would be legitimate,
                        although, as you say, this object isn't described later on; their
                        mentioning the EventListener function makes no doubt possible however,
                        the "correct", and not "preferred" , way is indeed to use a function.


                        Regards,
                        Yep.

                        Comment

                        • Yann-Erwan Perio

                          #13
                          Re: fireEvent

                          znndrp wrote:

                          [color=blue]
                          > Sorry :), I've got this htmledit object which treates a single [enter]
                          > as '</p><p>' and a [shift]+[enter] as '<br>'. My goal is to make this
                          > work the other way round[/color]
                          [color=blue]
                          > http://www.interactivetools.com/products/htmlarea/
                          >
                          > The function I'm hacking is the 'editor_event() ' function in 'editor.js'[/color]

                          That's a pretty huge script, however at the function you've pointed out
                          there's commented code; changing this code a little seems to give the
                          results you're looking for:

                          if (ord == 13 && editEvent.type == 'keypress') {
                          editEvent.retur nValue = false;
                          editor_insertHT ML(objname, shiftKey ? "<p>" : "<br>");
                          return;
                          }

                          I haven't (nor will) read the whole script so cannot tell whether this
                          will be safe; only the author could tell:-)

                          The conception behind the script is very strange, the author mixes
                          timeouts with event management, which is something that should never be
                          done; ISTM that she/he has drawn incorrect conclusions about events
                          concurrency, resulting, in the end, in the program flow getting quite
                          complicated.


                          HTH,
                          Yep.

                          Comment

                          • znndrp

                            #14
                            Re: fireEvent

                            Yann-Erwan Perio wrote:
                            [color=blue]
                            > znndrp wrote:[color=green]
                            >> http://www.interactivetools.com/products/htmlarea/
                            >>
                            >> The function I'm hacking is the 'editor_event() ' function in 'editor.js'[/color]
                            >
                            > That's a pretty huge script, however at the function you've pointed out
                            > there's commented code; changing this code a little seems to give the
                            > results you're looking for:[/color]
                            ....

                            Yes, I've noticed that piece of code, but if I uncommented that, the
                            UnOrderedList button doesn't work correctly anymore, so that's not an
                            acceptable solution.
                            [color=blue]
                            > I haven't (nor will) read the whole script so cannot tell whether this
                            > will be safe; only the author could tell:-)[/color]

                            I'm now expanding the authors code you pointed me to, so that it _will_ work
                            correctly with UnOrderedList.

                            --
                            ,_,
                            (O,O)
                            ( )
                            ---"-"-alex

                            Comment

                            • Yann-Erwan Perio

                              #15
                              Re: fireEvent

                              znndrp wrote:
                              [color=blue]
                              > Yes, I've noticed that piece of code, but if I uncommented that, the
                              > UnOrderedList button doesn't work correctly anymore, so that's not an
                              > acceptable solution.[/color]

                              Then try the following hack instead :

                              if (ord == 13 && editEvent.type == 'keypress') {
                              if(editor_obj.t agName.toLowerC ase()=="iframe" ) {
                              if (
                              (function(){
                              var el=editor_obj.c ontentWindow.do cument.selectio n.
                              createRange().p arentElement();
                              var lc;
                              while((lc=el.no deName.toLowerC ase())!="body") {
                              if(lc!="p") break;
                              el=el.parentNod e;
                              }
                              return lc=="body";
                              })()
                              ) {
                              editEvent.retur nValue = false;
                              editor_insertHT ML(objname, shiftKey ? "<p>" : "<br>");
                              return;
                              }
                              }
                              }

                              (ugly, eh?)


                              HTH,
                              Yep.

                              Comment

                              Working...