Solved

Window MouseLeave

Posted on 2011-03-09
5
161 Views
Last Modified: 2013-11-19
I'm trying to detect when the mouse leaves the window with jQuery 1.5.1.

The code below fires an event every time I mouse over an element on the page but even when I move the mouse out of the window the e.target is never the window object.  So it fires at all the wrong times and never at the right time.

    $(window).bind("mouseleave", function(e) {
        console.log(e.target);
    });


What am I doing wrong?

0
Comment
Question by:phasevar
  • 2
5 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 35081753

Try something like this instead:

<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26873652.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

$( function() {

  $(document).hover( eventHover );

});

function eventHover( event ) {
  $("#debug").append( $("<div/>").text( JSON.stringify(event) ) );
}

</script>
</head>
<body>

<div id="debug">
</div> 

</body>
</html>

Open in new window

0
 

Author Comment

by:phasevar
ID: 35082126
I can't seem to get hover to work with document, window or even 'body'.  Works with all other elements.

<script type="text/javascript">
$(document).ready(function() {
    console.log('starting test...');
    $(window).hover(function(e) {
        console.log('in:'+e.target);
    }, function(e) {
        console.log('out:'+e.target);
    });
});
</script>

Open in new window

0
 

Accepted Solution

by:
phasevar earned 0 total points
ID: 35083958
Found it.

<script type="text/javascript">
$(document).ready(function() {
    $(window).mouseout(function(e) { console.log('mouseout'); });
});
</script>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now