Over which window is the mouse?

Discussion in 'Javascript' started by Thomas Mlynarczyk, May 14, 2005.

  1. Hi,

    I have a JavaScript application which opens a second window. Which is the
    best way to detect over which (if any) of the two windows the mouse is?
    First I thought document.onmouseout and document.onmouseover (for both
    windows) would be the way to do it, but some browsers (like Firefox) seem to
    fire these even when the mouse passes within the document (like from the
    "empty body background" to a div), and anyway, any child elements of
    document would "get" the events before the document (event bubbling), so my
    script would still "think" the mouse is over the other window. Using
    onfocus/onblur does not seem to be the right idea either as it would require
    something like a click in a window to trigger them. Now the only more or
    less promising way I can think of is to set a reference to the respective
    window on each object I want to handle events for. But then I'd have to take
    care about the cyclic references this would create. So what could I do? Is
    there a simple solution?

    Thanks in advance for your advice,
    Thomas
    Thomas Mlynarczyk, May 14, 2005
    #1
    1. Advertising

  2. Thomas Mlynarczyk

    Jay Guest

    "Thomas Mlynarczyk" <> wrote in message
    news:d65ncn$q8i$03$-online.com...
    > Hi,
    >
    > I have a JavaScript application which opens a second window. Which is the
    > best way to detect over which (if any) of the two windows the mouse is?


    I might be missing something but wouldn't you want to check which window had
    focus?

    Jay


    > First I thought document.onmouseout and document.onmouseover (for both
    > windows) would be the way to do it, but some browsers (like Firefox) seem
    > to
    > fire these even when the mouse passes within the document (like from the
    > "empty body background" to a div), and anyway, any child elements of
    > document would "get" the events before the document (event bubbling), so
    > my
    > script would still "think" the mouse is over the other window. Using
    > onfocus/onblur does not seem to be the right idea either as it would
    > require
    > something like a click in a window to trigger them. Now the only more or
    > less promising way I can think of is to set a reference to the respective
    > window on each object I want to handle events for. But then I'd have to
    > take
    > care about the cyclic references this would create. So what could I do? Is
    > there a simple solution?
    >
    > Thanks in advance for your advice,
    > Thomas
    >
    >
    >
    Jay, May 15, 2005
    #2
    1. Advertising

  3. Thomas Mlynarczyk

    Guest

    Hi Thomas,

    I think that I have a solution for you.

    1) Attach an onMouseMove event to the body of both windows.
    2) The handler functions should change a variable which will indicate
    the "Hovered" window name or id

    than you can check this variable to get your answer.
    from the second window use window.opener.handlerName

    I Tried this solution on firefox ... and it's OK.

    good luck
    , May 15, 2005
    #3
  4. Also sprach Jay:

    > "Thomas Mlynarczyk" <> wrote in message
    > news:d65ncn$q8i$03$-online.com...
    >> Hi,
    >>
    >> I have a JavaScript application which opens a second window. Which
    >> is the best way to detect over which (if any) of the two windows the
    >> mouse is?

    >
    > I might be missing something but wouldn't you want to check which
    > window had focus?


    If window A has the focus and I simply move the mouse (no button pressed, no
    clicking, nor anything) to window B - would that alone change the focus
    already?
    Thomas Mlynarczyk, May 15, 2005
    #4
  5. Also sprach :

    > 1) Attach an onMouseMove event to the body of both windows.
    > 2) The handler functions should change a variable which will indicate
    > the "Hovered" window name or id


    Thank you for this solution. There is just one point that bothers me: Say I
    have a div in the second window which is right at the edge of the canvas. If
    I move the mouse over that window so that it enters where the div is,
    wouldn't the div get the onmouseover event first - before the event has
    bubbled up to the document and thus before the "whichWindow" variable has
    been changed?
    Thomas Mlynarczyk, May 15, 2005
    #5
  6. Thomas Mlynarczyk

    Guest

    Thomas,
    It is true that the DIV will catch the event before the body and than
    the event will bubble up to the body.

    what exactly are you trying to achieve?
    , May 15, 2005
    #6
  7. Also sprach :

    > Thomas,
    > It is true that the DIV will catch the event before the body and than
    > the event will bubble up to the body.
    >
    > what exactly are you trying to achieve?


    Well, among other things I want to update an object with mouse position
    information and in order to calculate that, it is necessary for some
    browsers to use document.scrollLeft and similar properties and that's why I
    need to know which document object to use.

    Meanwhile I have come up with another idea:

    object.onSomeEvent = function (e) { return window.opener.myEventHandler(e ||
    null, window, this); };

    If I'm not wrong the anonymous function should always be evaluated in the
    context of the window in which it was called and therefore the window
    parameter should point to the object's window. For objects in the main
    window, I would assign myEventHandler directly. I just wonder if this is
    really the best way to do this or if there are drawbacks (performance
    penalty due to nested function calls).
    Thomas Mlynarczyk, May 15, 2005
    #7
  8. Thomas Mlynarczyk

    Guest

    A creative idea!
    I guess [event.x , event.y , event.screenX , event.screenY] are not
    cross browser compatible ?

    anyway, I hope I helped.
    , May 16, 2005
    #8
  9. Also sprach :

    > A creative idea!


    Not creative enough, it seems. My test showed that the anonymous function
    will use the "window" in which it was defined rather than the one from which
    it was called. I'd have to define it in the new window, which is a bit
    awkward as I want to keep all the code in my main window and use the same
    syntax for every element, no matter in which window it is. So, in theory,
    the simplest thing would be to "backtrace" the window of an element using
    the parentNode property in a loop. But that would mean a huge performance
    penalty, I guess. So, I still haven't found a solution yet.

    Ah, yes, with the onfocus - the problem is, to get the event object, IE uses
    window.event, so I must know *which* window before I can get it. Also, I
    have some functions doing display-related things (like adding a node to the
    document tree) and these need to know the window as well (for stuff like
    document.getElementById()). But as these could be called independently of
    any event, an event-based solution would not work.

    > I guess [event.x , event.y , event.screenX , event.screenY] are not
    > cross browser compatible ?


    event.screenX and event.screenY yes, the others not. Every browser has its
    own way of providing mouse position information, so I actually use a rather
    complex thing like "if A is supported, use it, else if B is supported, use
    that etc.". This way, I can get the mouse position relative to the screen,
    the viewport, the document and the element.
    Thomas Mlynarczyk, May 16, 2005
    #9
  10. Thomas Mlynarczyk wrote:

    > If window A has the focus and I simply move the mouse (no button pressed,
    > no clicking, nor anything) to window B - would that alone change the focus
    > already?


    That depends on the window framework and it entirely possible.


    PointedEars
    --
    Wieso? Das ist die hochentwickelte Sensorik des OS.
    Es erkennt ja auch, wenn der Nutzer gerade wichtige Aufgaben zu
    erledigen hat und zeigt dies dann prägnant mit einem großen blauen
    grafischen Bedienelement an. ;-) -- Andreas Borutta in dciwam
    Thomas 'PointedEars' Lahn, May 22, 2005
    #10
  11. Thomas Mlynarczyk

    Randy Webb Guest

    Thomas 'PointedEars' Lahn wrote:
    > Thomas Mlynarczyk wrote:
    >
    >
    >>If window A has the focus and I simply move the mouse (no button pressed,
    >>no clicking, nor anything) to window B - would that alone change the focus
    >>already?


    No. The act, alone, of mousing over a window does not give it focus. Put
    an onfocus event handler and test it. Giving a window an onmouseover
    event handler to grab focus when its moused over would cause it to have
    focus.

    >
    > That depends on the window framework and it entirely possible.


    Not simply by mousing over the window, there has to be more involved
    than that.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Randy Webb, May 23, 2005
    #11
  12. Thomas Mlynarczyk

    Random Guest

    Mr. Lahn has a point: Some shells and window managers WILL
    automatically move the focus as a window is moused-over. This is most
    common on Linux-based operating systems, but you'll also see it
    frequently in any *NIX GUI shell (including some configurations of Mac
    OS X). I'm actually using a Windows shell (BlackBox for Windows) that
    does allow me to do that.

    Just something to keep in mind.

    I'd go with Mr. Webb's solution of an onMouseOver handler that grabs
    focus.
    Random, May 23, 2005
    #12
  13. Thomas Mlynarczyk

    Randy Webb Guest

    Random wrote:
    > Mr. Lahn has a point: Some shells and window managers WILL
    > automatically move the focus as a window is moused-over. This is most
    > common on Linux-based operating systems, but you'll also see it
    > frequently in any *NIX GUI shell (including some configurations of Mac
    > OS X). I'm actually using a Windows shell (BlackBox for Windows) that
    > does allow me to do that.
    >
    > Just something to keep in mind.
    >
    > I'd go with Mr. Webb's solution of an onMouseOver handler that grabs
    > focus.


    Would you also go with Mr. Webb's standard practice of quoting what I am
    replying to? After all, it *is* the recommended practice in this group.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Randy Webb, May 23, 2005
    #13
  14. Thomas Mlynarczyk

    Random Guest

    Now that Mr. G has informed him how to get Google Groups to do so, Mr.
    R would consider it, yes, but not so much on the strength of Mr. Webb's
    recommendation as on the courtesy of Mr. G's request.

    -R

    -------------------------------------------
    Sarcastic little bit, isn't he, that Mr. ---.
    -------------------------------------------
    Mr. Pink: How about if I'm Mr. Purple? That sounds good to me, I'll be
    Mr. Purple.
    Joe: You're not Mr. Purple. Some guy on some other job is Mr. Purple.
    You're Mr. Pink!
    Mr. White: Who cares what your name is?
    Mr. Pink: Yeah that's easy for you to say, you're Mr. White, you have a
    cool sounding name. All right look if it's no big deal to be Mr. Pink,
    do you wanna trade?

    -- From the script of Reservoir Dogs, by Quentin Tarantino.
    http://www.imdb.com/title/tt0105236/quotes
    Random, May 23, 2005
    #14
  15. Random wrote:

    > Mr. Lahn has a point: Some shells and window managers WILL
    > automatically move the focus as a window is moused-over. This
    > is most common on Linux-based operating systems, but you'll
    > also see it frequently in any *NIX GUI shell (including some
    > configurations of Mac OS X).


    Exactly.

    > I'm actually using a Windows shell (BlackBox for Windows) that
    > does allow me to do that.


    With Microsoft PowerToys' Control Panel feature,
    no special Windows shell is required for that.


    PointedEars
    Thomas 'PointedEars' Lahn, May 23, 2005
    #15
    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. Davisro
    Replies:
    2
    Views:
    1,306
    Jim Corey
    Apr 23, 2004
  2. Max
    Replies:
    7
    Views:
    9,098
  3. Claus Atzenbeck
    Replies:
    1
    Views:
    1,081
    Claus Atzenbeck
    Nov 26, 2004
  4. =?Utf-8?B?am9qb2Jhcg==?=
    Replies:
    1
    Views:
    4,082
    wwwtar
    Oct 2, 2006
  5. OdedDV
    Replies:
    7
    Views:
    153
    OdedDV
    Jun 5, 2005
Loading...

Share This Page