简体   繁体   English

如何使用javascript将焦点返回到父窗口?

[英]how return focus on parent window using javascript?

is it possible return focus on tab parent using javascript?是否可以使用 javascript 将焦点返回到选项卡父级? I read some threads about this problem, but i didn't find solutions.我阅读了一些有关此问题的主题,但没有找到解决方案。 I tried我试过

window.opener.parent.focus() window.opener.parent.focus()

and

window.opener.focus(); window.opener.focus();

but it doesn't work.但它不起作用。 Can someone help me?有人能帮我吗?

Thanks谢谢

Generally, you cannot do this inside a web page.通常,您不能在网页中执行此操作。 Because it's the user's choice which tab/window she wants to focus on and browsers such as firefox and chrome respect such choices by providing configs to open new tabs in the background or not.因为用户可以选择她想要关注的选项卡/窗口,而 Firefox 和 chrome 等浏览器通过提供在后台打开新选项卡的配置来尊重这些选择。 But under several very special cases, you may still achieve this.但是在几个非常特殊的情况下,您仍然可以实现这一点。

  1. If you want to open a new tab and return focus immediately, you can try to simulate a 'ctrl+click' event on a link to open the tab on the background.如果您想打开一个新选项卡并立即返回焦点,您可以尝试在链接上模拟一个 'ctrl+click' 事件以在后台打开该选项卡。 Refer to this thread Open a new tab in the background?请参阅此线程在后台打开一个新选项卡? ( Only for chrome, API may already changed. So it may only works on an obsoleted version ) 仅适用于 chrome,API 可能已经更改。因此它可能仅适用于过时的版本

  2. If you are shipping with an extension, do it in the extension code.如果您使用扩展程序发货,请在扩展程序代码中执行此操作。 For example: in chrome extension .例如:在 chrome 扩展中

  3. If your script is for a customized browsers which you have control on / you can affect the design, you can implement the function in the browser side and expose an API for your script.如果您的脚本用于自定义浏览器,您可以控制/您可以影响设计,您可以在浏览器端实现该功能并为您的脚本公开 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM