Simulate Mouse Event

Discussion in 'Javascript' started by vunet.us@gmail.com, Aug 8, 2007.

  1. Guest

    Hello,
    My function gets mouse coordinates like this:
    function getMouseCoords(e){
    e = (e) ? e : window.event;
    var x = 0;
    var y = 0;
    if(e.pageX){
    x = e.pageX;
    y = e.pageY;
    }else if(e.clientX){
    x = e.clientX + document.body.scrollLeft - document.body.clientLeft;
    y = e.clientY + document.body.scrollTop - document.body.clientTop;
    }
    return new Array(x, y);
    }

    To run the function I pass event to it:
    obj.onclick = runMe;
    function runMe(ev){
    ev = ev || window.event;
    var mousecoords = getMouseCoords(ev);
    }

    Question: how to simulate or avoid using "ev" variable in runMe() but
    get mouse coordinates anyway? I simply want to run runMe() in many
    places and subroutines, so passing event seems to be a complicated
    task.
    Thanks
     
    , Aug 8, 2007
    #1
    1. Advertising

  2. wrote:
    > [...]
    > To run the function I pass event to it:
    > obj.onclick = runMe;
    > function runMe(ev){
    > ev = ev || window.event;
    > var mousecoords = getMouseCoords(ev);
    > }
    >
    > Question: how to simulate or avoid using "ev" variable in runMe() but
    > get mouse coordinates anyway?


    Not possible. An event listener has a built-in fixed signature.

    > I simply want to run runMe() in many places and subroutines, so
    > passing event seems to be a complicated task.


    Even with event capturing, if you pass `event' from an event handler
    attribute to runMe(), the lines

    e = (e) ? e : window.event;

    in getMouseCoords() and

    ev = ev || window.event;

    in runMe() are unnecessary.

    However, you are looking for event bubbling, where you would need only one
    event listener:

    <head>
    <!-- ... -->
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function handleClick(e)
    {
    if (e)
    {
    var mousecoords = getMouseCoords(e);
    }
    }
    </script>
    </head>

    <body onclick="handleClick(e)">
    <!-- ... -->
    </body>

    See http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling


    HTH

    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Aug 8, 2007
    #2
    1. Advertising

  3. Guest

    On Aug 8, 5:28 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > wrote:
    > > [...]
    > > To run the function I pass event to it:
    > > obj.onclick = runMe;
    > > function runMe(ev){
    > > ev = ev || window.event;
    > > var mousecoords = getMouseCoords(ev);
    > > }

    >
    > > Question: how to simulate or avoid using "ev" variable in runMe() but
    > > get mouse coordinates anyway?

    >
    > Not possible. An event listener has a built-in fixed signature.
    >
    > > I simply want to run runMe() in many places and subroutines, so
    > > passing event seems to be a complicated task.

    >
    > Even with event capturing, if you pass `event' from an event handler
    > attribute to runMe(), the lines
    >
    > e = (e) ? e : window.event;
    >
    > in getMouseCoords() and
    >
    > ev = ev || window.event;
    >
    > in runMe() are unnecessary.
    >
    > However, you are looking for event bubbling, where you would need only one
    > event listener:
    >
    > <head>
    > <!-- ... -->
    > <meta http-equiv="Content-Script-Type" content="text/javascript">
    > <script type="text/javascript">
    > function handleClick(e)
    > {
    > if (e)
    > {
    > var mousecoords = getMouseCoords(e);
    > }
    > }
    > </script>
    > </head>
    >
    > <body onclick="handleClick(e)">
    > <!-- ... -->
    > </body>
    >
    > Seehttp://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling
    >
    > HTH
    >
    > PointedEars
    > --
    > Prototype.js was written by people who don't know javascript for people
    > who don't know javascript. People who don't know javascript are not
    > the best source of advice on designing systems that use javascript.
    > -- Richard Cornford, cljs, <f806at$ail$1$>


    but "e" in <body onclick="handleClick(e)"> is not defined, is it?!
     
    , Aug 8, 2007
    #3
  4. Guest

    is addEventListener something I may be considering?
     
    , Aug 8, 2007
    #4
  5. wrote:
    >> [...]

    >
    > but "e" in <body onclick="handleClick(e)"> is not defined, is it?!


    It isn't. Replace that `e' with `event'. WFM in MSHTML, Gecko, Opera.

    And please trim your quotes.


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Aug 8, 2007
    #5
  6. wrote:
    > is addEventListener something I may be considering?


    No, it would require full support for DOM Level 2 Events, and be overkill
    anyway (unless you were not dealing with [X]HTML). IE still doesn't have
    that, for example.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Aug 8, 2007
    #6
  7. David Mark Guest

    On Aug 8, 6:04 pm, wrote:
    > is addEventListener something I may be considering?


    If you need multiple listeners for the click event. You would need to
    do some feature detection as IE does not support addEventListener (it
    uses attachEvent.)

    Also, realize that your solution will fail in IE unless your document
    triggers quirks mode.

    You need to check for the layout mode (document.compatMode), which
    determines whether document.body or document.documentElement will
    provide the scroll and client offsets.

    Furthermore, it will fail in other browsers if pageX is 0. You need
    to check if pageX is a number.
     
    David Mark, Aug 9, 2007
    #7
  8. Guest


    >
    > > but "e" in <body onclick="handleClick(e)"> is not defined, is it?!

    >
    > It isn't. Replace that `e' with `event'. WFM in MSHTML, Gecko, Opera.
    >
    > And please trim your quotes.
    >

    thanks! works!
     
    , Aug 9, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael J Whitmore

    Simulate Mouse/Keyboard to stop Screen Saver

    Michael J Whitmore, Nov 6, 2003, in forum: Python
    Replies:
    1
    Views:
    1,378
    Dave Brueck
    Nov 6, 2003
  2. tom arnall
    Replies:
    6
    Views:
    1,309
    Ian Shef
    Jan 18, 2007
  3. Replies:
    4
    Views:
    365
  4. cluelis via .NET 247

    How to Simulate Click Event on DG LinkButton?

    cluelis via .NET 247, May 23, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    291
    Alvin Bruney [MVP]
    May 23, 2004
  5. Kid Kid
    Replies:
    6
    Views:
    524
Loading...

Share This Page