473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How can I detect when the users mouse leaves the document (body)

Jezternz
145 100+
Okay,
so I am working on a web application, that has lots of events, and I need to be able to trigger a function (probably through an event firing). Whenever a user leaves the page with the mouse. I have a timer being triggered every x milliseconds and I want to be able to cancel this interval when the users mouse leaves the page.
I have tried the onmouseout, onblur on the window, document, body and the web application division (note the web application div covers 100% of the document).
None of these work, I also tried triggering the event when the mouse got within 5 pixels of the edge of the document, however this was not reliable and even if it was, Its not really what I want.
*also note this only has to work with Google Chrome.

Cheers, Josh
May 18 '10 #1
5 2434
YarrOfDoom
1,247 Expert 1GB
I don't know how you're linking your function to your event, but with addEventListener() I've found you have to use mouseout instead of onmouseout (analogue for the other events).
May 18 '10 #2
Jezternz
145 100+
I am not so concerned with the event code, I have been using these functions I made (doesn't matter if its on<event> or <event>):
Expand|Select|Wrap|Line Numbers
  1. function addEvent(elem, etype, func){
  2.     if(!etype || etype.constructor != String || !func || func.constructor != Function)return false;
  3.     if(etype.indexOf("on") == 0)etype.replace("on","");
  4.     if(window.addEventListener)elem.addEventListener(etype,func,false);
  5.     else if(window.attachEvent)elem.attachEvent('on'+etype,func);
  6.     else return 0;
  7.     return 1;
  8. }
  9.  

My question is more a general javascript question, which element or object should I use and which event on that object should I use, if I want to trigger a function when the user moves the mouse outside of the document?

cheers, Josh
May 18 '10 #3
@Jezternz
Hi Josh,
It sounds like you want to attach the event listener to the parent of all the other elements. The HTML element from which all the other elements are descended is <body> so I think that is the element you're looking for. Alternatively you could try using the window object.

For the other part of your question, YarrOfDoom gave a good answer: your script will be listening for the mouseout event.

Happy coding!
May 19 '10 #4
Jezternz
145 100+
thanks for replys,
The problem I am having is, when I attach the mouseout event to the window or the body, it does not fire the event unless, I move the mouse off the window very slowly (so it is not really plausable, as often you will move the mouse out of the window in a fast motion). So are there any other ways I can go about this?

cheers, Josh
May 19 '10 #5
Jezternz
145 100+
I would like to apologize, I had something wrong with my code. I was under the impression the onmouseout event would not register on the window object, when infact it works perfectly.

Sorry for any time I wasted, Josh
May 19 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mat | last post by:
How can I detect when a link has been clicked but the new page is still in the process of loading? The document.location.href property still displays the current location (understandably) not the...
3
by: red | last post by:
mouse events when the mouse is on a "child control" hi everyone; my problem: I have a userControl in this usercontrol, I have a child control (a button) when the mouse moves over the...
2
by: Sam | last post by:
Hi, I can't figure out how to detect when my mouse cursor leaves a panel control. It should not trigger the event (or do anything) when the mouse leave the panel but still is over a control that...
3
by: Iosy | last post by:
Hi all, I'm working in a application where I need to detect when the mouse is inactive outside my form. I mean, I want to detect if the mouse hasn't move in the last X seconds (2 por example)...
1
by: scott8000 | last post by:
hi all Access 2K/2003, Win XP Pro I need to be able to detect when a subform datasheet is currently displaying a vertical scrollbar. I.e if the subform datasheet is showing more records...
2
by: =?Utf-8?B?SGFyZHkgV2FuZw==?= | last post by:
Hi all, I created a complex ASPX page, nested inside a master page and Ajax UpdatePanel. In one of my Javascript function I need to capture mouse position. I use alert(document.body.scrollTop) to...
4
by: windandwaves | last post by:
Hi Folk On one of my sites, I have a reather involved page (searchable map). When the person leaves the page then all the AJAX generated information is lost. Is there a way that I can retain...
4
by: kiransasi | last post by:
hi all, i m herewith struck with this problem.... check out the following code and lemme know the solution for this.... <html> <heAD> <TITLE>WAIT</TITLE>
4
by: yawnmoth | last post by:
I'm trying to write a script whereby the body background is changed to back (from white) when the mouse button is held down and kept held down. When you lift it up, I'd like it to change back to...
0
by: Frank Rizzo | last post by:
I have a form with a lot of controls on it. How can I detect when the mouse leaves the form? I've tried wiring up a MouseLeave event for every single control, but that does not work because those...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.