javascript 如何让 window.focus() 在 Firefox 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22052388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get window.focus() to work in firefox
提问by Kevin Op den Kamp
Does anyone have a clue on how to be able to use window.focus()
in FireFox? (27.0.1).
I have tried all the about:config settings out there but still no luck.
有没有人知道如何window.focus()
在 FireFox 中使用?(27.0.1)。我已经尝试了所有 about:config 设置,但仍然没有运气。
My code looks something like this:
我的代码看起来像这样:
var wi = window.open('myUrl');
wi.focus();
== EDIT ==
== 编辑 ==
I'm trying to make this work in FireFox on my machine, not for any other users so please refrain from the standard "let your users decide" or "it depends on the user's settings" as I want to get this to work for a local project :)
我试图在我的机器上的 FireFox 中完成这项工作,而不是为任何其他用户,所以请不要使用标准“让你的用户决定”或“这取决于用户的设置”,因为我想让它为一个本地项目:)
采纳答案by turing_machine
Tools > Options, Content area, "Advanced" button to the right of "Enable JavaScript" should give you the option to allow this behavior. If you're on Linux, the navigation might be slightly different.
工具 > 选项、内容区域、“启用 JavaScript”右侧的“高级”按钮应该为您提供允许此行为的选项。如果您使用的是 Linux,导航可能会略有不同。
Note, it is generally considered best practice to allow the user to choose what window should be in focus.
请注意,允许用户选择应该聚焦的窗口通常被认为是最佳实践。
EDIT:
编辑:
This is about:config you're referring to: dom.disable_window_flip
这是 about:config 你指的是:dom.disable_window_flip
In FF 27.0.1 I am able to open a new window with focus by providing window name and option parameters to window.open, and then calling focus.
在 FF 27.0.1 中,我可以通过向 window.open 提供窗口名称和选项参数,然后调用焦点来打开一个具有焦点的新窗口。
var wi = window.open('http://www.google.com', 'window_name', 'height=200,width=200');
wi.focus();
Example here: http://jsfiddle.net/CLVh2/
这里的例子:http: //jsfiddle.net/CLVh2/
For Chrome support, you'll want to check out: Google Chrome "window.open" workaround?
对于 Chrome 支持,您需要查看:Google Chrome“window.open”解决方法?
EDIT: FOR TAB SUPPORT
编辑:对于标签支持
If you want to open a tab and give the tab focus in FF (27.0.1), and simply calling window.open('url') is not giving you the desired result, try checking the "When I open a new tab, switch to it immediately" option. It can be found in Tools > Options > Tabs
如果您想打开一个选项卡并在 FF (27.0.1) 中设置选项卡焦点,并且简单地调用 window.open('url') 并没有给您想要的结果,请尝试检查“当我打开一个新选项卡时,立即切换到它”选项。它可以在工具 > 选项 > 选项卡中找到
回答by rpax
According to Window.focusspecification from Mozilla, that's not possible. It depends on the user's settings.
根据Mozilla 的Window.focus规范,这是不可能的。这取决于用户的设置。
Makes a request to bring the window to the front. It may fail due to user settingsand the window isn't guaranteed to be frontmost before this method returns.
请求将窗口放在前面。由于用户设置,它可能会失败,并且在此方法返回之前不能保证窗口位于最前面。