How to detect if a window is out of focus?

Discussion in 'Javascript' started by Charles, Jul 19, 2006.

  1. Charles

    Charles Guest

    Is there a way to detect if a window is out of focus?

    Here's what I need it for: I have a web-based chat application that one
    of the 2 people chatting may have the chat window open or minimized.

    When the chat window is open, probably it's because the person is
    reading the other person's messages or because he/she is typing a
    message. When the chat window is minimized it's probably because none
    of the guys have chatted over the last minutes, and probably because no
    one is typing.

    Now, if some one sends a message to the other guy, I want the
    recipient's minimized window to blink in his task bar so that he knows
    he's got a new message. To do this, I would remotely ask to give focus
    to his window. But if by chance the recipient is typing a message, his
    window isn't out of focus, and if I ask to give focus to his window,
    his textarea where he's writing will lose focus, and he won't be able
    to type anymore until he manually gives focus again to the textarea
    where he was typing.

    Do you get the picture? How do I know if a window is out of focus?
    Thanks,
    Charles, Jul 19, 2006
    #1
    1. Advertising

  2. Charles

    Erwin Moller Guest

    Charles wrote:

    > Is there a way to detect if a window is out of focus?
    >
    > Here's what I need it for: I have a web-based chat application that one
    > of the 2 people chatting may have the chat window open or minimized.
    >
    > When the chat window is open, probably it's because the person is
    > reading the other person's messages or because he/she is typing a
    > message. When the chat window is minimized it's probably because none
    > of the guys have chatted over the last minutes, and probably because no
    > one is typing.
    >
    > Now, if some one sends a message to the other guy, I want the
    > recipient's minimized window to blink in his task bar so that he knows
    > he's got a new message. To do this, I would remotely ask to give focus
    > to his window. But if by chance the recipient is typing a message, his
    > window isn't out of focus, and if I ask to give focus to his window,
    > his textarea where he's writing will lose focus, and he won't be able
    > to type anymore until he manually gives focus again to the textarea
    > where he was typing.
    >
    > Do you get the picture? How do I know if a window is out of focus?
    > Thanks,


    Why not just keep track of it?
    Like this:

    <html>
    <head>

    <script type="text/javascript">
    self.setTimeout("showFocus();",100);

    var Blurred = false;

    function showFocus(){

    // get all windowproperties.
    var theDiv = document.getElementById("cont");

    var theContent = "Blurred=";
    if (Blurred){
    theContent += "TRUE<br>";
    } else {
    theContent += "FALSE<br>";
    }

    theDiv.innerHTML = theContent;

    self.setTimeout("showFocus();",1000);
    }


    </script>


    </head>
    <body onBlur="Blurred=true;" onFocus="Blurred=false;">
    <div id="cont">
    cont here
    </div
    </body>
    </html>


    If you run that simple page you will see the Blurred= will change with the
    focus/blur.

    Regards,
    Erwin Moller
    Erwin Moller, Jul 20, 2006
    #2
    1. Advertising

  3. Charles

    Charles Guest

    Thank you Erwin, this did the trick :)
    Charles, Jul 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    4
    Views:
    1,617
    Jason
    May 7, 2007
  2. Randell D.
    Replies:
    8
    Views:
    216
    keyur shah
    Nov 6, 2003
  3. Barry Svee

    Using onblur to detect loss of window focus

    Barry Svee, Dec 31, 2003, in forum: Javascript
    Replies:
    1
    Views:
    240
    Thomas 'PointedEars' Lahn
    Jan 11, 2004
  4. Roger
    Replies:
    3
    Views:
    322
  5. Spizzat2
    Replies:
    2
    Views:
    177
    Spizzat2
    Jan 9, 2008
Loading...

Share This Page