Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  101] [ 1]  / answers: 1 / hits: 16327  / 15 Years ago, wed, november 4, 2009, 12:00:00

I'd like some Javascript code to run when the mouse leaves the browser window. I only need to support Safari (WebKit.)



I tried putting a mouseout handler on window. That handler is reliably called when the mouse leaves the browser window. But because of bubbling it is also called when the mouse moves between elements in the document. I can't figure out how to determine when the mouse has actually left the window and when it has only moved between elements.



When the mouse leaves the window, exactly one event is generated, and the target element appears to be the element the mouse was actually over. So checking to see if the target element is window or document doesn't work. And wrapping the whole page in an invisible containing div doesn't work either: if the div is invisible, then the mouse will never be over it, so nothing changes.



(The same thing happens if I put the handler on document or document.body, except that surprisingly document.body does not get mouseover/mouseout events when the mouse enters or leaves an empty part of the window, such as the empty vertical space created by absolutely positioning an element with bottom:0. For that space, document and window will get mouseover/mouseout events with the target being <html>, but document.body will not.)



Some ideas I had:




  • On each mouseout event, get the actual position of the mouse and see if it is in fact over the window. But I don't know if this is actually possible and it sounds like it would be tricky to eliminate all of the race conditions.

  • Also register a mouseover handler and detect cases where mouseout is not proceeded by (or followed shortly by a) a mouseover. But that would require a timer.



We use prototype.js so ideally I'd like to express the solution in terms of prototype's Event.observe, but I can figure that part out.



Thanks for any suggestions!


More From » ajax

 Answers
76

SUMMARY: This can be done cleanly by checking the relatedTarget property during the mouseout event. If relatedTarget is not a child of document, then the mouse just left the window. It's easy to do yourself, but if you don't want to, some libraries (Mootools, future Prototype..) have baked-in functionality, and others (current Prototype) have extensions available. On IE, you could instead use mouseleave, which is a non-bubbling version of mouseout.



Details:



IE has events called mouseenter and mouseleave that are non-bubbling versions of mouseover and mouseout. Other browsers do not, but if they did, setting a mouseleave listener on window or document would do the trick.



A gentleman named Ken Snyder comes to the rescue:




On a mouseover, the relatedTarget
property references the node from
which the pointer came. On a mouseout,
the relatedTarget property references
the node to which the pointer went.On
any event, the scope is the node to
which the event is attached.When the
relatedTarget is a not child of the
currentTarget, a mouseover event is
equivalent to a mouseenter event and a
mouseout event is equivalent to a
mouseleave event.




-- http://kendsnyder.com/archives/6-MouseEnter-and-MouseLeave.html



This makes it possible to implement mouseenter and mouseleave in other browsers. In fact, Ken provides same Prototype code to do so: http://kendsnyder.com/sandbox/enterleave/MouseEnterLeave.js



Duroth pointed out in comments that MooTools already includes something similar. (Thanks Duroth.) It sounds like the upcoming Prototype release (1.6.2) may include this functionality, but I can't find anything definite.


[#98393] Thursday, October 29, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;