views:

10760

answers:

10

How do I make a user's browser blink/flash/highlight in the task bar using JavaScript? For example, if I make an AJAX request every 10 seconds to see if the user has any new messages on the server, I want the user to know it right away, even if he is using another application at the time.

Edit: These users do want to be distracted when a new message arrives.

+1  A: 

The only way I can think of doing this is by doing something like alert('you have a new message') when the message is received. This will flash the taskbar if the window is minimized, but it will also open a dialog box, which you may not want.

A: 

Why not take the approach that GMail uses and show the number of messages in the page title?

Sometimes users don't want to be distracted when a new message arrives.

andyuk
+1  A: 

These users do want to be distracted when a new message arrives.

It sounds like you're writing an app for an internal company project.

You might want to investigate writing a small windows app in .net which adds a notify icon and can then do fancy popups or balloon popups or whatever, when they get new messages.

This isn't overly hard and I'm sure if you ask SO 'how do I show a tray icon' and 'how do I do pop up notifications' you'll get some great answers :-)

For the record, I'm pretty sure that (other than using an alert/prompt dialog box) you can't flash the taskbar in JS, as this is heavily windows specific, and JS really doesn't work like that. You may be able to use some IE-specific windows activex controls, but then you inflict IE upon your poor users. Don't do that :-(

Orion Edwards
+1  A: 

you could change the title of the web page with each new message to alert the user. I did this for a browser chat client and most users thought it worked well enough.

document.title = "[user] hello world";
Toran Billups
+3  A: 

My "user interface" response is: Are you sure your users want their browsers flashing, or do you think that's what they want? If I were the one using your software, I know I'd be annoyed if these alerts happened very often and got in my way.

If you're sure you want to do it this way, use a javascript alert box. That's what Google Calendar does for event reminders, and they probably put some thought into it.

A web page really isn't the best medium for need-to-know alerts. If you're designing something along the lines of "ZOMG, the servers are down!" alerts, automated e-mails or SMS messages to the right people might do the trick.

Rudi
+1  A: 

You may want to try window.focus() - but it may be annoying if the screen switches around

Sugendran
+15  A: 

this won't make the taskbar button flash in changing colours, but the title will blink on and off until they move the mouse. This should work cross platform, and even if they just have it in a different tab.

function newExcitingAlerts() {
    var oldTitle = document.title;
    var msg = "New!";
    var timeoutId = setInterval(function() {
        document.title = document.title == msg ? ' ' : msg;
    }, 1000);
    window.onmousemove = function() {
        clearInterval(timeoutId);
        document.title = oldTitle;
        window.onmousemove = null;
    };
}

Update: You may want to look at using HTML5 notifications. Currently, AFAIK only Chrome supports this, but the other good browsers should follow suit soon enough.

nickf
I could not get this to work as described in IE 8. It kept blinking the title forever. Instead of using onmousemove, I had to use onfocus and onblur to keep track of when the window was in focus or not and stop the blinking in the onfocus function.So when the page loads, I register onfocus and onblur functions that toggle a boolean "focused" variable. I have another boolean to track when the blinking has started. In the onfocus, if the blinking has started, I stop it.
Peter M
window.onmousemove won't work in IE, however document.onmousemove does work. Using onblur and onfocus works pretty good as well, but there is a strange bug in Chrome when setting document.title in an onfocus event handler. See my blog post about it if you want: http://heyman.info/2010/oct/7/google-chrome-bug-when-setting-document-title/
heyman
+5  A: 

Supposedly you can do this on windows with the growl for windows javascript API:

http://ajaxian.com/archives/growls-for-windows-and-a-web-notification-api

Your users will have to install growl though.

Eventually this is going to be part of google gears, in the form of the NotificationAPI:

http://code.google.com/p/gears/wiki/NotificationAPI

So I would recommend using the growl approach for now, falling back to window title updates if possible, and already engineering in attempts to use the Gears Notification API, for when it eventually becomes available.

Joeri Sebrechts
A: 

AFAIK, there is no good way to do this with consistency. I was writing an IE only web-based IM client. We ended up using window.focus(), which works most of the time. Sometimes it will actually cause the window to steal focus from the foreground app, which can be really annoying.

Chase Seibert
A: 

I've made a jQuery plugin for the purpose of blinking notification messages in the browser title bar. You can specify different options like blinking interval, duration, if the blinking should stop when the window/tab gets focused, etc. The plugin works in Firefox, Chrome, Safari, IE6, IE7 and IE8.

Here is an example on how to use it:

$.titleAlert("New mail!", {
    requireBlur:true,
    stopOnFocus:true,
    interval:600
});

If you're not using jQuery, you might still want to look at the source code (there are a few quirky bugs and edge cases that you need to work around when doing title blinking if you want to fully support all major browsers).

heyman