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?