2010-11-04 17 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基于)。

更好的解决方案欢迎。

相关问题