View Full Version : Is Window Active?
Hi all,
does anybody know how to tell programmatically if window is active or not?
The window where script is executing. My goal is to play sound alerts only when window is not active (not focused) to drag attention.
Isn't that what Gmail chat uses? See http://googleblog.blogspot.com/2006/05/stop-hey-whats-that-sound.html
They use Flash for the sound. Maybe they also rely on Flash to tell whether the window is active or not?
But... I'm thinking now that you could perhaps use the onfocus and onblur events of the window, to keep the current state in a variable.
Currently I'm sticking to this approach, i.e. storing state in a member var and updating it in onblur/onfocus.
The problem is that these event handlers work inconsistently in IE/Firefox.
IE inconsistencies:
In IE window.onblur is fired when you change focus between divs inside your window. Thus, if you click in some div, the onblur will be fired, and the state will be set to "inactive" which is obviously a bug.
I found a solution here:
http://pro-thoughts.blogspot.com/2006/10/incorrect-behavior-of-windowonblur.html
Really good trick. But there is still minor inconsistency. When you minimize your browser window, and then maximize it back, the onfocus is not fired. Subsequent minimize's/maximize's don't fire any event.
Firefox inconsistencies
It seems that when you click inside different divs onfocus is called for each click. Which is not a problem for me, though.
Now, if you click inside a textarea and switch to another app -- onblur is not fired.
Finally, it seems that Firefox fires each event twice when you switch between the browser and another app.
These are all of the issues I have found so far.
Hope I will be able to handle them.
Animal
6 Jun 2007, 11:08 PM
Your website is going to make noises at me if I visit?
What's the URL, so I can remember not to go there? ;)
Seriously, I don't think this is a good design decision. It annoys people. It only takes a small annoyance for people to just drop viewing your page.
I'm developing live help tool and it's critical to play a short "ding" when a new message arrives. Because by that moment you might have switched to another application (support forum, or email client). It's necessary to notify online rep that site visitor has just sent a message.
The url is http://www.xoochat.com/ :)
But it's not the site where you will hear sounds. It's operator console, a web-based app, where sounds are necessary.
Animal
6 Jun 2007, 11:47 PM
Ah right, that's probably justifiable. I was just thinking of some awful sites where they play noodling, electronic tunes.
Ah right, that's probably justifiable. I was just thinking of some awful sites where they play noodling, electronic tunes.
Ah :) I hate them as well. They really disturb.
The problem is that these event handlers work inconsistently in IE/Firefox.
I didn't know about these inconsistencies... but should have imagined, right? Life couldn't be suddenly so easy ;)
I didn't know either. But stumbled upon them while implementing some code :)
brian.moeskau
7 Jun 2007, 9:52 PM
If you absolutely must get the user's attention, have you considered a plain JS alert? It will act differently in different OSs (Mac = reactivate window, Win = I think will display the alert above other OS windows, or maybe flash the taskbar icon), but it will do something to get the user's attention visually. It has the added benefit of staying around in case the user was looking away, away from desk, etc.
Here's an easy test for what it will do. Run this in the Firebug console and then minimize Firefox:
setTimeout("alert('hello');", 5000);
If it is not critical enough to use an alert, then I would argue that it's not critical enough to play a sound either. Even if you get it to work, they might miss the sound (again, away from desk, etc.) then nothing will notify them that anything had happened. If this level of passivity is acceptable, then you should just let the user return to the window when they choose to anyway and not bother them with a sound.
Either way, I don't think trying to hack a sound into your page is the best approach.
I have to admin I don't agree with you.
1) Alerts are synchronous, which is no no for my application.
2) Once you use any live help tool for at least a day you will understand that sounds are crucial.
Another good grabber is flashing button in the taskbar, but I'm not sure if it's possible to do in JavaScript.
Anyway, thanks for your ideas.
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.