Javascript window.focus(), self.focus() 在 Firefox 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2533305/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:47:12  来源:igfitidea点击:

window.focus(), self.focus() not working in firefox

javascriptjqueryjquery-plugins

提问by Nisanth Kumar

Hi all i am developing a chat application ... i have multiple chat windows ... i want to know which windw contain new message ... i have the following code ..

大家好,我正在开发一个聊天应用程序......我有多个聊天窗口......我想知道哪个窗口包含新消息......我有以下代码......

function getCount()
{
    $.ajax({
       type: "POST",
       url: baseUrl + '/Chat/count',
       data: "chat_id=" + document.ajax.chat_id.value,
       success: function(msg){
                if(msg == 'new1') {
                    self.focus();
                                            //window.focus();

                }
            }
    });
}

If an operator attending both chat....

如果一个接线员参加两个聊天......

for example the url is like http://localhost/nisanth/admin/Chat/index/chatId/15http://localhost/nisanth/admin/Chat/index/chatId/16

例如网址就像 http://localhost/nisanth/admin/Chat/index/chatId/15 http://localhost/nisanth/admin/Chat/index/chatId/16

http://localhost/nisanth/user/Chat/index/chatId/15http://localhost/nisanth/user/Chat/index/chatId/16

http://localhost/nisanth/user/Chat/index/chatId/15 http://localhost/nisanth/user/Chat/index/chatId/16

if the user 16enter a message i need focus

如果user 16输入消息我需要关注

http://localhost/nisanth/admin/Chat/index/chatId/16

This code is work fine with IE but not in firefox...please give me a solution... the above code is in the same html

此代码适用于 IE,但不适用于 firefox...请给我一个解决方案...上面的代码在同一个 html 中

回答by Pointy

Firefox will only obey requests to raise a window if a security option is set, and it's not set by default. Chrome won't pay attention to focus() requests at all, as far as I can tell. Safari does obey focus() request.

如果设置了安全选项,Firefox 只会服从引发窗口的请求,并且默认情况下未设置。据我所知,Chrome 根本不会关注 focus() 请求。Safari 确实遵守 focus() 请求。

The specific Firefox setting is in the "Tools" -> "Options" ("Edit -> Preferences" on Linux, maybe MacOS) dialog. There's a "Content" tab, and in that there's a checkbox for enabling Javascript. Along with thatis an "Advanced" button that brings up anotherdialog, wherein one finds a checkbox to allow (or disallow) the raising and lowering of windows by page code.

特定的 Firefox 设置位于“工具”->“选项”(Linux 上的“编辑 -> 首选项”,也可能是 MacOS)对话框中。有一个“内容”选项卡,其中有一个用于启用 Javascript 的复选框。随着是一个“高级”按钮,带来了另一个对话框,其中一个发现一个复选框,以允许(或不允许)的升高和通过页代码降低窗口。

edit:Here is a test page: http://gutfullofbeer.net/focus1.htmland you should be able to see that Firefox willraise a window when the page calls window.focus(). You must either have the browser set up so that new windows (created with window.open()) open up in a new separate window instead of a tab, or else you can tear off the tab of the secondary page when it opens.

编辑:这是一个测试页面:http: //gutfullofbeer.net/focus1.html,您应该能够看到 Firefox在页面调用时弹出一个窗口window.focus()。您必须设置浏览器,以便window.open()在新的单独窗口而不是选项卡中打开新窗口(使用 来创建),否则您可以在辅助页面打开时撕下该选项卡。

回答by Shessuky

I had the same problem, i though there was a problem with my JS script, after a long search i have found a solution:

我遇到了同样的问题,虽然我的 JS 脚本有问题,但经过长时间的搜索,我找到了解决方案:

1) In a new tab, type about:configin the address bar and press button "Enter". Click the button "I accept the risk!" in order to confirm the warning

1) 在新选项卡中,在地址栏中键入about:config并按“Enter”按钮。点击按钮“我接受风险!” 为了确认警告

2) In the search field, type dom.disableto get dom.disable_window_flip

2) 在搜索栏中,输入dom.disable得到dom.disable_window_flip

3) If the property dom.disable_window_flipis true, double-click it to switch the value from true to false

3)如果属性dom.disable_window_flip为true,双击它,将值由true切换为false