By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

How to detect if a window is out of focus?

P: n/a
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,

Jul 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 20 '06 #2

P: n/a
Thank you Erwin, this did the trick :)

Jul 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.