Solved

Window MouseLeave

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

839 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