Java 小程序在离开浏览器并返回时会永久失去焦点
我有一个网页,其中applet是唯一一个类似以下内容的元素:Java 小程序在离开浏览器并返回时会永久失去焦点,java,javascript,swing,browser,applet,Java,Javascript,Swing,Browser,Applet,我有一个网页,其中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> <a
<!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>
...
Alt文本
当页面最初加载时,焦点在小程序中设置,我可以很好地通过选项卡与小程序交互。但是,如果我离开浏览器窗口,然后回到它,我就不能再仅仅使用tab键重新聚焦小程序
按F5重新加载页面将修复页面,使小程序重新获得焦点,但此解决方案不可接受
我如何解决这个问题?谢谢。暂定解决方案:
//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;
其余的代码只是在加载页面后附加焦点处理窗口(使用脚本JQuery.ready是基于的)
欢迎使用更好的解决方案。我希望“初始焦点”参数仅适用于小程序初始加载时。大概当您导航到另一个选项卡/页面并返回时,小程序会丢失焦点,因此它不会自动重新获得焦点。OTOH注意到添加了mayscript标志,您可以使用基于JavaScript的解决方案在页面再次激活时将焦点返回给小程序。@Andrew,事实上,initial_focus参数并没有给我任何帮助,因为小程序似乎在加载时默认获得了焦点。是的,我在使用document.AppletName.requestFocus()时取得了一定的成功,但我正在努力找到理想的事件/策略,用于检测小程序何时没有焦点,然后调用requestFocus。我对JavaScript没有太多钻研,所以我没有什么好主意(不管怎样,Java程序员几乎是这个星球上问JS最糟糕的人)。我猜你可以在你的帖子中添加一个javascript标签?如果是这样,你可能会这样做&引起一些JS大师的注意。
if(!document.AppletName.isActive())
document.AppletName.requestFocus();