Why does 'window.Alert()' work in tabbed navigation and 'window.Focus()' does not?

Asked

Viewed 243 times

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 on the tag body:

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

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

Only I wish I could do the same without using Alert, it’s possible?

I tried with the window.focus(). But nothing has happened! Is there any other way?

  • This varies from browser to browser. In Microsoft Edge for example, neither alert returns the previous page, nor the focus.

  • 1

    No focus, no warning. It’s horrible from a user experience point of view when you’re browsing and a page keeps interrupting manipulating the window. Although even Google Calendar does this. The best solution is to use some notifications API. The OS itself has this in the chat, where it skips a pop-up when you receive new message.

1 answer

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.