I want to be able to detect when the mouse leaves the window so I can stop events from firing while the user's mouse is elsewhere.
Any ideas of how to do this?
I want to be able to detect when the mouse leaves the window so I can stop events from firing while the user's mouse is elsewhere.
Any ideas of how to do this?
This type of behavior is usually desired while implementing drag-drop behavior on an html page. The solution below was tested on IE 8.0.6, FireFox 3.6.6, Opera 10.53, and Safari 4 on an MS Windows XP machine.
First a little function from Peter-Paul Koch; cross browser event handler:
And then use this method to attach an event handler to the document objects mouseout event:
Finally, here is an html page with the script embedded for debugging:
None of these answers worked for me. I'm now using:
I'm using this for a drag and drop file uploading widget. It's not absolutely accurate, being triggered when the mouse gets to a certain distance from the edge of the window.
This might be a bit hacky but it will only trigger when the mouse leaves the window. I kept catching child events and this resolved it
I've tried all the above, but nothing seems to work as expected. After a little research I found that e.relatedTarget is the html just before the mouse exits the window.
So ... I've end up with this:
Please let me know if you find any issues or improvements !
Maybe if you're constantly listening to OnMouseOver in the body tag, then callback when the event is not ocurring, but, as Zack states, this could be very ugly, because not all the browsers handle events the same way, there is even some possibility that you lose the MouseOver even by being over a div in the same page.
This will work in chrome,