108

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.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
erik
  • 6,406
  • 3
  • 36
  • 36

12 Answers12

89

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.

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

Update: You may want to look at using HTML5 notifications.

lance
  • 16,092
  • 19
  • 77
  • 136
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 1
    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 Mar 25 '10 at 20:11
  • 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 Oct 07 '10 at 17:50
  • 4
    This doesn't appear to work in Chrome... I don't think Chrome understands that the empty string is anything. If I use a hyphen as the "blank" message, it works fine. – John B Apr 28 '11 at 16:48
  • 1
    this seems to stack if the alert is triggered multiple times. this results in the interval getting faster and faster, then when you remove, it only removes a single interval. – Horse Jun 22 '12 at 10:45
  • 2
    In Windows 7, changing the title doesn't blink/flash in the taskbar – Chand Apr 29 '13 at 21:20
  • Thanks for the heads-up about HTML5 notification. The link seems out-of-date, though; it doesn't work in the latest version of Chrome. The [MDN reference](https://developer.mozilla.org/en-US/docs/Web/API/notification) worked, though. – Sam Mar 31 '15 at 23:10
  • 1
    How do you pass arguments (the message) to this? – shinzou Oct 17 '16 at 15:42
  • In Windows 10, changing the title doesn't show due to small tile in taskbar and it doesn't blink/flash as well. Any Solution for this? – Ambuj Khanna Jul 02 '18 at 10:39
56

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
  • 4,845
  • 3
  • 26
  • 19
6
                var oldTitle = document.title;
                var msg = "New Popup!";
                var timeoutId = false;

                var blink = function() {
                    document.title = document.title == msg ? oldTitle : msg;//Modify Title in case a popup

                    if(document.hasFocus())//Stop blinking and restore the Application Title
                    {
                        document.title = oldTitle;
                        clearInterval(timeoutId);
                    }                       
                };

                if (!timeoutId) {
                    timeoutId = setInterval(blink, 500);//Initiate the Blink Call
                };//Blink logic 
Rikki Goswami
  • 69
  • 1
  • 1
5

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
  • 11,012
  • 3
  • 35
  • 50
2

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
  • 541
  • 3
  • 11
  • 15
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – secretformula Jun 26 '14 at 14:55
  • 2
    @secretformula is it really necessary to dig out 5 year old posts and flag them as low-quality? – Taifun Jun 26 '14 at 14:58
  • 3
    @Taifun it came up in the VLQF queue so yes it is. Meta has discussed this too – secretformula Jun 26 '14 at 15:00
  • @secretformula, this actually does provide a very good answer to the question: use a JavaScript `alert`. – Sam Mar 31 '15 at 22:54
  • wrong or not, this doesn't look like an answer to me, but history shows that 2 moderators already declined the NAA flag, so I'm following consensus here. – Jean-François Fabre May 07 '19 at 12:39
2

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.

Robin Barnes
  • 13,133
  • 15
  • 44
  • 45
  • 1
    Inconsistent across current browsers - each behaves differently and none make the taskbar icon flash (tested Win8 - IE10, Chrome, Firefox) – danwellman Dec 11 '12 at 14:40
1

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

Sugendran
  • 2,099
  • 1
  • 14
  • 15
1

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
  • 38,118
  • 16
  • 52
  • 52
1

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
  • 27,111
  • 40
  • 155
  • 268
1
function blinkTab() {
        const browserTitle = document.title;
        let timeoutId;
        let message = 'My New Title';

        const stopBlinking = () => {
            document.title = browserTitle;
            clearInterval(timeoutId);
        };

        const startBlinking = () => {
            document.title = document.title  === message ? browserTitle : message;
        };

        function registerEvents() {
            window.addEventListener("focus", function(event) { 
                stopBlinking();
            });

            window.addEventListener("blur", function(event) {
                const timeoutId = setInterval(startBlinking, 500);
            });
        };

        registerEvents();
    };


    blinkTab();
prashantsahni
  • 2,128
  • 19
  • 20
0

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
  • 15,703
  • 8
  • 51
  • 58
-1

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
  • 121,657
  • 64
  • 239
  • 328