dynamically add onmousemove function with event as attribute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    dynamically add onmousemove function with event as attribute

    <body onmousemove="mo veMe()">

    This function can also be called as
    document.onmous emove = function {moveMe();};

    Similarly what can be done for onmousemove="mo veMe(event)" ?
  • Jezternz
    New Member
    • Jan 2008
    • 145

    #2
    I am interested in the answer to this also, I recently had the same issue. I just took a shortcut by storeing the "event" in a global variable, but if theres another way I would like to know.
    Thanks, Josh

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      i think you mean something like the following?

      [CODE=javascript]function my_event(e) {
      alert('test ' + e.target.tagNam e);
      }

      window.document .onclick = function(event) {
      return function(event) { my_event(event) };
      }();[/CODE]
      you should always avoid the assignment as a string since this is eval-like and never needed, even in the setTimeout and setInterval methods. but the best way would be to use attachEvent() for IE and addEventListene r() for the other browsers ... when it comes to event-handling.

      note: the example works in FF/Mozilla - IE has no target, but you may use srcElement here ... ;)

      kind regards

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Thank you Gits.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          no problem ... i just hate that eval and eval-like things ... since it is not needed as i said and makes the code quite ugly :) there are needs for it (when JSON is to be evaled) but even that might be obsolete in the future ... have a look here in case you are interested :)

          kind regards

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Did not work on ie.. :(

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Don't forget that IE uses the global window.event. Use something like this to get the target.

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                Originally posted by acoder
                Don't forget that IE uses the global window.event. Use something like this to get the target.
                Thanks, so it finally worked like this..[code=javascript] document.onmous emove = function (event) {
                if (!event) event = window.event;
                drag(event);
                };[/code]

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Yes, and another alternative:
                  [code=javascript]event = event || window.event;[/code]

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Thats better, will use this one ...
                    [code=javascript]drag(event || window.event);[/code]

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      yep ... i just misguided you the first time ... the orig suggestion should have been:

                      [CODE=javascript]function my_event(e) {
                      alert('test ' + e.target.tagNam e);
                      }

                      window.document .onclick = function(event) { my_event(event) };
                      [/CODE]
                      of course. the outer wrapping function was useless of course ... was a copy&paste err because first i had another idea that didn't work ... so: copy&paste is evil ... i always knew that :) and i missed to mention the use of window.event in IE ... so it just was a bad answer that time ;)) please forgive me .... :D

                      kind regards

                      Comment

                      • hsriat
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1653

                        #12
                        awww... if I were a girl, I would have said, "so sweet..."

                        But I actually knew what you mean. And I already did that, but just wasn't aware of window.event

                        Cheers...

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5390

                          #13
                          :D if i were a girl i would have said ... 'didn't you have seen what i meant? ... always when i tell you something you don't understand me, don't understand what i mean ... you are such an ignorant man ... just listen to me and always read between the lines too ... do i just have to tell you everything?' ... *lol*

                          Comment

                          • hsriat
                            Recognized Expert Top Contributor
                            • Jan 2008
                            • 1653

                            #14
                            LOL!! so true... and If you had replied like that, I would have understood you are a girl, and would have replied you, "Sorry, its my fault. How could I be a dumb! ".
                            Its not something to get upset at. Its their nature, God made it like this. We shouldn't question Him.

                            Thanks gits, I had a good time in reading and replying.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Haha, you guys had better watch yourselves. If any member/viewing guest of the fairer gender happens to pass by, handbags could go a-flying :D

                              Comment

                              Working...