[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Window MouseLeave

Posted on 2011-03-09
5
Medium Priority
?
173 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
3 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

873 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