window.opener.focus() 不起作用 [英] window.opener.focus() doesn't work

查看:41
本文介绍了window.opener.focus() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法让它发挥作用.

I can't seem to get this to work.

响应单击,窗口 A 打开窗口 B(然后具有焦点).然后,响应单击 B,窗口调用 window.opener.focus(),但焦点不会回到 A.

In response to a click, window A opens window B (which then has focus). Then, in response to a click on B, the window calls window.opener.focus(), but the focus does not go back to A.

我发现了一个奇怪的、奇怪的 Chrome 解决方法(29,可能还有其他的).如果我跑:

I have found a strange, strange workaround for Chrome (29, possibly others). If I run:

window.opener.name = 'somename';
window.open(window.opener.location.href, window.opener.name);
window.opener.focus();

它确实有效(并且不会重新加载窗口 A).但这对 Firefox 不起作用,而且可能是侥幸.

it does work (and doesn't reload window A). But this doesn't work for Firefox, and it is probably a fluke anyway.

我似乎很清楚openerfocus应该做什么,但是window.opener.focus() 不起作用.我错过了什么?

It seems very clear to me what opener and focus are supposed to do, but window.opener.focus() doesn't work. What am I missing?

推荐答案

来自 精美手册:

发出请求将窗口放在前面.由于用户设置,它可能会失败,并且不能保证在此方法返回之前窗口位于最前面.

Makes a request to bring the window to the front. It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.

强调我的.调用 focus() 只是一个请求,浏览器可以随意忽略你,你通常应该期望被忽略.如果您需要某些原因让浏览器忽略您的请求,请考虑在有人打字时将焦点切换到一个小窗口,您可能会遇到哪些邪恶的事情.

Emphasis mine. Calling focus() is just a request and the browser is free to ignore you and you should generally expect to be ignored. Consider what sorts of nefarious things you could get up to by switching focus to a tiny window while someone is typing if you need some reasons why a browser would ignore your request.

如果您需要 focus() 来使您的应用程序正常工作,那么您需要重新设计您的应用程序,使其不需要调用 focus().

If you need focus() to work for your application to work then you need to redesign your application so that it doesn't need to call focus().

这篇关于window.opener.focus() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆