Solved

Window MouseLeave

Posted on 2011-03-09
5
158 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now