离开浏览器并返回时小程序永久失去焦点

发布于 2024-09-30 16:29:24 字数 727 浏览 1 评论 0原文

我有一个网页,其中一个小程序作为唯一的元素,看起来像这样:

<!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>

当页面最初加载时,焦点设置在小程序中,我可以通过选项卡浏览并与小程序交互。但是,如果我离开浏览器窗口然后返回它,我将无法再仅使用 Tab 键重新获得小程序的焦点。

按F5重新加载页面修复了页面,使Applet重新获得焦点,但这种解决方案是不可接受的。

我该如何解决这个问题?谢谢。

I have a web page with an applet as the only element that looks something like this:

<!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>

When the page initially loads, focus is set in the applet and I can tab through and interact with the applet just fine. However, if I leave the browser window and then come back to it, I can no longer regain focus on the applet just using the tab key.

Pressing F5 to reload the page fixes the page so that the Applet regains focus, but this solution is unacceptable.

How do I solve this problem? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气尐潴 2024-10-07 16:29:24

暂定解决方案:

//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 是基于)。

欢迎更好的解决方案。

Tentative solution:

//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;

Note that the key part for detecting whether the applet needs focus and requesting it if so is (this only works if mayscript is enabled):

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

The rest of the code is just attaching the window on focus handling after the page is loaded (using the script JQuery.ready is based off of).

Better solutions welcome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文