2010-08-11 77 views
0

我正在開發一個網絡聊天,當對話發生變化時我需要提出一個事件,以便頁面標題發生變化,以便一個用戶可以知道另一個用戶已經寫入任何內容。如何檢測用戶是否點擊了我的html頁面所在的頁面選項卡?

所以,我試圖的代碼:

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

和 的addEvent(文件, '焦點',函數(){警報(2);});

但它不起作用。

即使用戶未在網頁上單擊,我也需要提高事件

對於該事件,我已經得到了解決方案:

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

有沒有什麼解決辦法嗎?

在此先感謝。

回答

0

我發現這個代碼,工作對於IE6,7,8和Firefox,並不需要Jquery。

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

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

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

source

+0

喜。感謝代碼。在Firefox中工作,但它在IE 8中不起作用。 – 2010-08-12 08:31:09

0

摘自: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); 
} 

如果它不與你的addEvent工作,你可能想嘗試的jQuery(見Event for when user switches browser tabs

$(window).blur(function(e) { 
    // Do Blur Actions Here 
}); 
$(window).focus(function(e) { 
    // Do Focus Actions Here 
}); 
相關問題