Javascript 焦点选项卡或窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8135188/
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
Focus tab or window
提问by jAndy
for a little app, I'm opening a few windows/tabs from my script. Whether the browser opens a window or a tab is of course not in my hand.
对于一个小应用程序,我正在从我的脚本中打开几个窗口/选项卡。浏览器是打开窗口还是标签页当然不在我的掌握之中。
However, I hold the references to the newly created window
objects and I do change their content "remotely" from another window. This all happens under the same document.domain
so no xss problem.
但是,我持有对新创建window
对象的引用,并且我确实从另一个窗口“远程”更改了它们的内容。这一切都发生在相同的情况下,document.domain
所以没有 xss 问题。
The problem is, I cannot reliably focus
those created windows/tabs. Since I'm writing a very specific app for a customer, I'm only targeting Firefox as browser. One option I have is of course just to do a remoteWindow.alert('foobar');
to get bring that window/tab up front, but that is pretty ugly isn't it.
问题是,我无法可靠地focus
创建那些窗口/选项卡。由于我正在为客户编写一个非常具体的应用程序,因此我只将 Firefox 定位为浏览器。我当然有一个选择,就是remoteWindow.alert('foobar');
把那个窗口/选项卡放在前面,但这很丑,不是吗。
I found this answer How to focus window/tab like alert()?
我找到了这个答案How to focus window/tab like alert()?
and it's said there, that Firefox has an option to allow script focus. So finally my question is, what is that option ?
I searched the about:config
for "tabs" and "focus" but didn't find anything related.
据说在那里,Firefox 有一个允许脚本焦点的选项。所以最后我的问题是,那个选项是什么?我搜索了about:config
“标签”和“焦点”,但没有找到任何相关内容。
How to configure ?
如何配置?
采纳答案by zatatatata
The only solution I see, is to force the popup in a new window, since there doesn't seem to be a way to focus another tab. This solution also requires you to change the default Javascript security settings in Tools > Options > Content tab
and click on the Advanced
button next to Enable Javascript
checkbox and check the middle box to allow focusing windows.
我看到的唯一解决方案是在新窗口中强制弹出窗口,因为似乎没有办法聚焦另一个选项卡。此解决方案还要求您更改默认的 Javascript 安全设置Tools > Options > Content tab
并单击复选框Advanced
旁边的按钮Enable Javascript
并选中中间框以允许聚焦窗口。
To force the use of a window rather than a tab, use win = window.open("http://www.google.com", "test" ,"modal=yes");
and then call win.focus();
whenever you feel like it.
要强制使用窗口而不是选项卡,请随时使用win = window.open("http://www.google.com", "test" ,"modal=yes");
然后调用win.focus();
。
EDIT:Actually forgot to mention the fact that this is FF only.
编辑:实际上忘了提及这只是 FF 的事实。
回答by Alex
The following appears to work in IE8 and FF13:
以下似乎适用于 IE8 和 FF13:
<script type="text/javascript">
// Stupid script to force focus to an existing tab when the link is clicked.
// And yes, we do need to open it twice.
function openHelp(a) {
var tab = window.open(a.href, a.target);
tab.close();
tab = window.open(a.href, a.target);
return false;
}
</script>
<a href="help.html" target="help" onclick="return openHelp(this);">Help</a>
回答by Kirill Dubovikov
See Mozilla's documentation: https://developer.mozilla.org/en/Code_snippets/Tabbed_browser.
请参阅 Mozilla 的文档:https: //developer.mozilla.org/en/Code_snippets/Tabbed_browser。
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var browserEnumerator = wm.getEnumerator("navigator:browser");
// Check each browser instance
while (browserEnumerator.hasMoreElements()) {
var browserWin = browserEnumerator.getNext();
var tabbrowser = browserWin.gBrowser;
// Check each tab of this browser instance
var numTabs = tabbrowser.browsers.length;
for (var index = 0; index < numTabs; index++) {
var currentBrowser = tabbrowser.getBrowserAtIndex(index);
if (/*some logic*/) {
// For an example
tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[index];
// Focus *this* browser-window
browserWin.focus();
break;
}
}
Here is an easier event-driven approach - https://developer.mozilla.org/En/Listening_to_events_on_all_tabs.
这是一个更简单的事件驱动方法 - https://developer.mozilla.org/En/Listening_to_events_on_all_tabs。
回答by pradeek
Have you tried window.focus()?
你试过window.focus()吗?
回答by skimberk1
There's no real way to do this for the simple reason that if websites had this ability, they'd end up using it to automatically focus ads or whatnot.
没有真正的方法可以做到这一点,原因很简单,如果网站有这种能力,他们最终会使用它来自动聚焦广告或诸如此类的东西。
It can be done, but only in Firefox IF the user has it enabled, which he most likely won't.
它可以做到,但只有在 Firefox 中,如果用户启用了它,他很可能不会这样做。
回答by Bergi
To answer your question: I've found http://www.gtalbot.org/FirefoxSection/Popup/PopupAndFirefox.html#RaiseLowerSetting, which says the setting you're looking for is called "allow raise and lower windows".
要回答您的问题:我找到了http://www.gtalbot.org/FirefoxSection/Popup/PopupAndFirefox.html#RaiseLowerSetting,它说您正在寻找的设置称为“允许升高和降低窗口”。
Another setting would be about:config/dom.disable_window_flip
(to false), found here.
另一个设置是about:config/dom.disable_window_flip
(设置为 false),在此处找到。
回答by harrymc
Have you tried in menu Tools -> Options… -> Content tab -> Advanced button next to "Enable JavaScript", to check the box named "Raise or Lower Windows" ?
您是否尝试过在菜单工具 -> 选项... -> 内容选项卡 -> “启用 JavaScript”旁边的高级按钮中,选中名为“升高或降低 Windows”的框?
An example on how to set the focus can be found here.
可以在此处找到有关如何设置焦点的示例。
回答by rhinoeli
It is dirty and ugly, so it is only plus information: if you call an alert() in any of the open tabs/windows, that tab will gain focus.
它又脏又丑,所以它只是附加信息:如果您在任何打开的选项卡/窗口中调用 alert(),该选项卡将获得焦点。
回答by Jules
Expanding on Alex's contribution above. The use case is having a CMS. When a site owner is logged into the CMS, and the front end is loaded in another tab. You give the site owner a link on the front end that reloads the CMS tab and focuses is.
扩展上面亚历克斯的贡献。用例是有一个 CMS。当站点所有者登录到 CMS 并且前端加载到另一个选项卡中时。您在前端为站点所有者提供一个链接,该链接可重新加载 CMS 选项卡和焦点。
Place this javacript on front end pages (or include in a global JS file):
将此 javacript 放在前端页面上(或包含在全局 JS 文件中):
<script type="text/javascript">
// Stupid script to force focus to an existing tab when the link is clicked.
// And yes, we do need to open it twice.
function loadAdmin(a) {
var tab = window.open(a.href, a.target);
tab.close();
tab = window.open(a.href, a.target);
return false;
}
</script>
I like to put the CMS link just after the H1 content. Again, this is only if they are logged into the CMS. In this case I am pointing to a Framed CMS, so it has to know what to load into the main frame (pun intended).
我喜欢把 CMS 链接放在 H1 内容之后。同样,这仅当他们登录到 CMS 时。在这种情况下,我指向一个 Framed CMS,因此它必须知道要加载到主框架中的内容(双关语)。
<h1>
Page Heading
<a href="/admin/main/?load=pages/page.cfm?id=#request.pageid#" title="Edit this page"
onclick="return loadAdmin(this);"
target="#cgi.server_name#wvAdmin"
><img src="/media/admin/icon-edit.png" alt="Edit this page"/></a>
</h1>
This to make sure the tab is unique, in case I have several CMSs in my browser (support multiple clients):
这是为了确保选项卡是唯一的,以防我的浏览器中有多个 CMS(支持多个客户端):
target="#cgi.server_name#wvAdmin"
目标=“#cgi.server_name#wvAdmin”
Finally, on the frame loader in the CMS, add this...
最后,在 CMS 中的帧加载器上,添加这个...
<script type="text/javascript">
window.name = '#cgi.server_name#wvAdmin';
</script>
... to ensure that if the user opened the CMS on their own, we can still work with it.
...确保如果用户自己打开了 CMS,我们仍然可以使用它。
回答by WTK
The simple answer is: you can't. Browsers doesn't expose that kind of API to javascript.
简单的答案是:你不能。浏览器不会向 javascript 公开这种 API。