views:

28

answers:

2

I'm developing a web chat and I need to raise an event when the conversation has changed so the page title changes so one user can know that the other user has written anything.

So I tried the code:

addEvent(window,'focus',function(){ alert(1); } );

and addEvent(document,'focus',function(){ alert(2); } );

but it does not work.

I need the event to be raised even if the user does not click on the web page.

For that event, I've got the solutions:

<body onclick="showClickedElement(this);" >

Is there any solution?

Thanks in advance.

A: 

taken from: http://bob.pythonmac.org/archives/2010/04/25/tab-visible-event/

var timer = null;
function tabVisible() {
    if (timer) clearInterval(timer);
    timer = null;
    window.onfocus = null;
    window.onmouseover = null;
    /* your code to dispatch event here */
}
// Firefox, IE8
window.onfocus = tabVisible;
// Safari
window.onmouseover = tabVisible;
// dirty hack for Chrome
if (navigator.userAgent.indexOf(' Chrome/') != -1) {
    function dirtyChromePoll() {
        if (window.screenX || window.screenY) tabVisible();
    }
    timer = setInterval(dirtyChromePoll, 100);
}

If it doesn't work with your addEvent, you might want to try jQuery (see http://stackoverflow.com/questions/1038643/event-for-when-user-switches-browser-tabs)

$(window).blur(function(e) {
    // Do Blur Actions Here
});
$(window).focus(function(e) {
    // Do Focus Actions Here
});
BGerrissen
A: 

I've found this code that works for IE6, 7,8 and firefox and no not need Jquery.

function onBlur() {
document.body.className = 'blurred';

}; function onFocus(){ document.body.className = 'focused'; };

if (/*@cc_on!@*/false) { // check for Internet Explorer document.onfocusin = onFocus; document.onfocusout = onBlur; } else { window.onfocus = onFocus; window.onblur = onBlur; }

source

Francisco Campos
hi. thanks for the code. works in firefox but it didn't work in IE 8.
Francisco Campos