Solved

Window MouseLeave

Posted on 2011-03-09
5
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp response.write to clients 6 34
how to customize the text in the legend in highcharts 3 35
Compute age Html 2 27
allow link tag to be submit like form 13 21
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 …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

730 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