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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jezternz
    New Member
    • Jan 2008
    • 145

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

    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
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    I don't know how you're linking your function to your event, but with addEventListene r() I've found you have to use mouseout instead of onmouseout (analogue for the other events).

    Comment

    • Jezternz
      New Member
      • Jan 2008
      • 145

      #3
      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>):
      Code:
      function addEvent(elem, etype, func){
      	if(!etype || etype.constructor != String || !func || func.constructor != Function)return false;
      	[U]if(etype.indexOf("on") == 0)etype.replace("on","");[/U]
      	if(window.addEventListener)elem.addEventListener(etype,func,false);
      	else if(window.attachEvent)elem.attachEvent('on'+etype,func);
      	else return 0;
      	return 1;
      }

      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

      Comment

      • thesmithman
        New Member
        • Aug 2008
        • 37

        #4
        Originally posted by Jezternz
        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>):
        Code:
        function addEvent(elem, etype, func){
        	if(!etype || etype.constructor != String || !func || func.constructor != Function)return false;
        	[U]if(etype.indexOf("on") == 0)etype.replace("on","");[/U]
        	if(window.addEventListener)elem.addEventListener(etype,func,false);
        	else if(window.attachEvent)elem.attachEvent('on'+etype,func);
        	else return 0;
        	return 1;
        }

        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
        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!

        Comment

        • Jezternz
          New Member
          • Jan 2008
          • 145

          #5
          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

          Comment

          • Jezternz
            New Member
            • Jan 2008
            • 145

            #6
            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

            Comment

            Working...