Over which window is the mouse?

T

Thomas Mlynarczyk

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
 
J

Jay

Thomas Mlynarczyk said:
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
 
S

shlomi.schwartz

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
 
T

Thomas Mlynarczyk

Also sprach Jay:
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?
 
T

Thomas Mlynarczyk

Also sprach (e-mail address removed):
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?
 
S

shlomi.schwartz

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?
 
T

Thomas Mlynarczyk

Also sprach (e-mail address removed):
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).
 
S

shlomi.schwartz

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

anyway, I hope I helped.
 
T

Thomas Mlynarczyk

Also sprach (e-mail address removed):
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.
 
T

Thomas 'PointedEars' Lahn

Thomas said:
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
 
R

Randy Webb

Thomas said:
Thomas Mlynarczyk wrote:

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.
 
R

Random

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.
 
R

Randy Webb

Random said:
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.
 
R

Random

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
 
T

Thomas 'PointedEars' Lahn

Random said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top