2010-11-04 48 views
0

我有一個網頁有一個applet的,看起來像這樣的唯一元素:小程序將永久失去焦點當離開瀏覽器,並回來

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <title>...</title> 
</head> 
<body> 
<applet style="padding:1px; border:1px solid gray" mayscript="mayscript" codebase="..." name="AppletName" code="..." archive="..." width="600" height="500" alt="Alt Text"> 
    <param name="initial_focus" value="true"/> 
    Alt Text 
</applet> 
</body> 
</html> 

當頁面初始加載,焦點設置在applet並且我可以選中並與applet進行交互。但是,如果我離開瀏覽器窗口然後再回到它,我不能再使用tab鍵重新關注小程序。

按F5重新加載頁面可修復頁面,以便Applet重新獲得焦點,但此解決方案是不可接受的。

我該如何解決這個問題?謝謝。

+0

我期望'initial_focus'參數僅用於applet最初加載時的工作。推測當你導航到另一個標籤/頁面時,焦點會丟失到小程序中,因此它不會自動重新獲得它。 OTOH注意到添加了mayscript標誌,您可能會尋找基於JavaScript的解決方案,以便在頁面再次激活時將焦點返回到小程序。 – 2010-11-04 03:42:39

+0

@Andrew的確,initial_focus參數並沒有真正給我提供任何東西,因爲applet似乎在默認情況下在加載時獲得焦點。是的,我已經使用document.AppletName.requestFocus()獲得了適度的成功,但我努力尋找理想的事件/策略來檢測applet何時沒有焦點,然後調用requestFocus。 – 2010-11-04 12:52:40

+0

我並沒有深入研究JavaScript,因此我沒有任何出色的想法(Java程序員幾乎是世界上最糟糕的人,無論如何都要問JS)。我猜測有一個JavaScript標籤,你可以添加到你的文章?如果是這樣,你可能會這樣做,並阻礙一些JS大師的注意力。 – 2010-11-04 13:50:05

回答

0

初步解決方案:

//Dean Edwards/Matthias Miller/John Resig 
function init() { 
    // quit if this function has already been called 
    if (arguments.callee.done) return; 

    // flag this function so we don't do the same thing twice 
    arguments.callee.done = true; 

    // kill the timer 
    if (_timer) clearInterval(_timer); 

    window.onfocus = function() { 
    if(!document.AppletName.isActive()) 
     document.AppletName.requestFocus(); 
    }; 
} 

/* for Mozilla/Opera9 */ 
if (document.addEventListener) { 
    document.addEventListener("DOMContentLoaded", init, false); 
} 

/* for Internet Explorer */ 
/*@cc_on @*/ 
/*@if (@_win32) 
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>"); 
    var script = document.getElementById("__ie_onload"); 
    script.onreadystatechange = function() { 
    if (this.readyState == "complete") { 
     init(); // call the onload handler 
    } 
    }; 
/*@end @*/ 

/* for Safari */ 
if (/WebKit/i.test(navigator.userAgent)) { // sniff 
    var _timer = setInterval(function() { 
    if (/loaded|complete/.test(document.readyState)) { 
     init(); // call the onload handler 
    } 
    }, 10); 
} 

/* for other browsers */ 
window.onload = init; 

注意,對於檢測小程序是否需要關注,並要求它,如果這樣的(如果MAYSCRIPT啓用此僅工程)的重要組成部分:

if(!document.AppletName.isActive()) 
    document.AppletName.requestFocus(); 

的其餘代碼只是在加載頁面後使用焦點處理附加窗口(使用腳本JQuery.ready基於)。

更好的解決方案歡迎。