Why does 'window.alert ()' work in the navigation between tabs and the 'window.focus ()' does not?

6

I have the following method:

(function(){
    window.addEventListener('blur', openChat);
    window.addEventListener('pagehide', openChat);

})();
function openChat(){
           setTimeout(function(){
             alert('Há uma mensagem nova!')
             document.getElementById('msg').innerHTML='Nova mensagem!';
          }, 4000);
}

And in the body tag:

<div id="msg">Mensagem antiga!</div>

If I change the tab in the browser, after 4 seconds, it returns to the tab I was before (focus), with the new message.

I just wish I could do the same without using alert, is it possible?

I've tried window.focus() . But nothing happened! Is there any other way?

    
asked by anonymous 12.04.2016 / 00:24

1 answer

1

Is there any other way?

Well, you can use api notifications to generate a desktop notification for the user.

A quick search by google, I found a question asked in the American stackoverflow, follows: link

A practical example of api: link

    
15.04.2016 / 19:05