window.onblur -- how does this work?

Discussion in 'JavaScript' started by Leon945, Jan 8, 2007.

  1. #1
    Hi all...
    I have a little problem.

    I want to be able to detect when the user moves away from the current active browser window.

    The reason i want to do this is because i have a dhtml menu which remains open no matter what.

    so this is what i want to do..


    window.onblur=function(){ 
          menuBar.closeAllMenus(); 
    }
    
    Code (markup):

    but the code isnt executed when i move away from the window!
    can someone help me with this?

    i've also tried this:


    
    window.onblur=function(){ 
          window.alert("AH!"); 
          closeMenus(); 
    } 
    
    function closeMenus(){ 
          window.alert("IN"); 
          menuBar.closeAllMenus(); 
    } 
    </script> 
    </HEAD> 
    
    <body topmargin="0" leftmargin="0" onblur="closeMenus()">
    Code (markup):
    and its not working

    I cant post the whole menu code because its VERY long, but i know the
    menuBar.closeAllMenus() instruction works.. i've tried it with

    function closeMenus(){ 
          menuBar.closeAllMenus(); 
    } 
    setInterval(closeMenus,1000);
    
    Code (markup):

    and sure enough every second all the menus are closed.

    is there another way to know if the user is leaving the window?

    right now the page is divided into 2 frames, im working with the top frame where the menu is.

    does that affect the behaviour of window.onblur ?
     
    Leon945, Jan 8, 2007 IP
  2. EvilivE

    EvilivE Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This works:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="Author" content="Allmar Technologies, www.allmartech.com" />
    <title>OnBlur</title>
    <script type="text/javascript">
    function focusMoved() {
    	alert("See you later.");
    }
    </script>
    </head>
    <body onBlur="focusMoved();">
    </body>
    </html>
    
    HTML:
    Since your using frames, you would place the onBlur in your frameset instead of the body as it is above.

    Also note that this does cause a loop since it is popping up an alert box, which takes the focus away from the web page.
     
    EvilivE, Jan 8, 2007 IP