window.focus()无法在Google Chrome中使用

nic*_*ine 46 javascript google-chrome cross-browser dom-events

只是想知道谷歌Chrome是否会window.focus()在某些时候支持.当我的意思是支持时,我的意思是让它发挥作用.对它的调用不会失败,它只是没有做任何事情.所有其他主流浏览器都没有这个问题:FireFox,IE6-IE8和Safari.

我有一个用于管理浏览器窗口的客户端类.当我第一次创建一个窗口时,窗口就会聚焦,但随后将焦点带到窗口的尝试不起作用.

据我所知,这似乎是一个安全功能,以避免恼人的弹出窗口,它似乎不是一个WebKit问题,因为它在Safari中工作.

我知道有人提出的一个想法是关闭窗户然后重新打开它,但这是一个可怕的解决方案.谷歌搜索表明,我似乎并不是唯一一个对此感到沮丧的人.

只是要100%清楚,我的意思是新窗口,而不是标签(标签不能从我读过的内容中聚焦),所有打开的窗口都在同一个域中.

除了我上面提到的坏事之外,还有什么想法,解决方法吗?

Chromium项目中记录了一个关于此问题的错误,请在此处查看.感谢发布Rich.

MyCompany = { UI: {} }; // Put this here if you want to test the code. I create these namespaces elsewhere in code.

MyCompany.UI.Window = new function() {
    // Private fields
    var that = this;
    var windowHandles = {};

    // Public Members
    this.windowExists = function(windowTarget) {
        return windowTarget && windowHandles[windowTarget] && !windowHandles[windowTarget].closed;
    }

    this.open = function(url, windowTarget, windowProperties) {
        // See if we have a window handle and if it's closed or not.
        if (that.windowExists(windowTarget)) {

            // We still have our window object so let's check if the URLs is the same as the one we're trying to load.
            var currentLocation = windowHandles[windowTarget].location;

            if (
                (
                    /^http(?:s?):/.test(url) && currentLocation.href !== url
                )
                    ||
                (
                    // This check is required because the URL might be the same, but absolute,
                    // e.g. /Default.aspx ... instead of http://localhost/Default.aspx ...
                    !/^http(?:s?):/.test(url) &&
                    (currentLocation.pathname + currentLocation.search + currentLocation.hash) !== url
                )
            ) {
                // Not the same URL, so load the new one.
                windowHandles[windowTarget].location = url;
            }

            // Give focus to the window. This works in IE 6/7/8, FireFox, Safari but not Chrome.
            // Well in Chrome it works the first time, but subsequent focus attempts fail,. I believe this is a security feature in Chrome to avoid annoying popups.
            windowHandles[windowTarget].focus();
        }
        else
        {
            // Need to do this so that tabbed browsers (pretty much all browsers except IE6) actually open a new window
            // as opposed to a tab. By specifying at least one window property, we're guaranteed to have a new window created instead
            // of a tab.
            windowProperties = windowProperties || 'menubar=yes,location=yes,width=700, height=400, scrollbars=yes, resizable= yes';
            windowTarget = windowTarget || "_blank";

            // Create a new window.
            var windowHandle = windowProperties ? window.open(url, windowTarget, windowProperties) : window.open(url, windowTarget);

            if (null === windowHandle) {
                alert("You have a popup blocker enabled. Please allow popups for " + location.protocol + "//" + location.host);
            }
            else {
                if ("_blank" !== windowTarget) {
                    // Store the window handle for reuse if a handle was specified.
                    windowHandles[windowTarget] = windowHandle;
                    windowHandles[windowTarget].focus();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 9

在Windows 7上的14.0.835.202版本中仍然相同; 找到了另一种解决方法,不是更优雅,但至少会避免丢失页面上的数据:在要关注的窗口中显示警报.

  • 在弹出窗口的情况下,这只会在警报关闭之前返回焦点。 (2认同)

Ahe*_*eho 9

我一直在努力解决这个问题.我想要引用另一个窗口,所以我发出了一个:

otherWinRef = window.open("","OtherWindow");
Run Code Online (Sandbox Code Playgroud)

但是,当我发出此命令时,浏览器会将焦点切换到OtherWindow.我认为这可以通过这样做来解决:

otherWinRef = window.open("","OtherWindow");
window.focus();
Run Code Online (Sandbox Code Playgroud)

但是window.focus()没有效果.我试过了:

otherWinRef = window.open("","OtherWindow");
setTimeout(window.focus,0);
Run Code Online (Sandbox Code Playgroud)

但是window.focus()调用仍然没有效果.

我通过将以下代码添加到OtherWindow的源代码来解决此问题.

function Bounce(w) {

        window.blur();
        w.focus();
}
Run Code Online (Sandbox Code Playgroud)

然后我将主窗口中的代码更改为:

otherWinRef = window.open("","OtherWindow");
otherWinRef.Bounce(window);
Run Code Online (Sandbox Code Playgroud)

  • 如果两个页面都属于你的话,那就有用......但是很好的技巧. (3认同)

nic*_*ine 7

更新:此解决方案似乎不再适用于Chrome.

令人难以置信的是,解决方案非常简单.我一直在试图解决这个问题至少一周.您需要做的就是模糊窗口然后给它焦点.我之前尝试过这个并没有用.

windowHandle.blur();
windowHandle.focus();
Run Code Online (Sandbox Code Playgroud)

所以我最终尝试了这个:

windowHandle.blur();
setTimeout(windowHandle.focus, 0);
Run Code Online (Sandbox Code Playgroud)

这似乎有效.

我在这里更新了我的代码:

MyCompany = { UI: {} }; // Put this here if you want to test the code. I create these namespaces elsewhere in code.

MyCompany.UI.Window = new function() {       
    // Private fields
    var that = this;
    var windowHandles = {};
    var isChrome = /chrome/.test(navigator.userAgent.toLowerCase());

    // Public Members
    this.focus = function(windowHandle) {
        if (!windowHandle) {
            throw new Exception("Window handle can not be null.");
        }

        if (isChrome) {
            windowHandle.blur();
            setTimeout(windowHandle.focus, 0);                    
        }
        else {
            windowHandle.focus();
        }    
    }

    this.windowExists = function(windowTarget) {
        return windowTarget && windowHandles[windowTarget] && !windowHandles[windowTarget].closed;
    }

    this.open = function(url, windowTarget, windowProperties) {
        // See if we have a window handle and if it's closed or not.
        if (that.windowExists(windowTarget)) {

            // We still have our window object so let's check if the URLs is the same as the one we're trying to load.
            var currentLocation = windowHandles[windowTarget].location;

            if (
                (
                    /^http(?:s?):/.test(url) && currentLocation.href !== url
                )
                    ||
                (
                    // This check is required because the URL might be the same, but absolute,
                    // e.g. /Default.aspx ... instead of http://localhost/Default.aspx ...
                    !/^http(?:s?):/.test(url) &&
                    (currentLocation.pathname + currentLocation.search + currentLocation.hash) !== url
                )
            ) {
                // Not the same URL, so load the new one.
                windowHandles[windowTarget].location = url;
            }

            // Give focus to the window. This works in IE 6/7/8, FireFox, Safari but not Chrome.
            // Well in Chrome it works the first time, but subsequent focus attempts fail,. I believe this is a security feature in Chrome to avoid annoying popups.
            that.focus(windowHandles[windowTarget]);
        }
        else {
            // Need to do this so that tabbed browsers (pretty much all browsers except IE6) actually open a new window
            // as opposed to a tab. By specifying at least one window property, we're guaranteed to have a new window created instead
            // of a tab.
            //windowProperties = windowProperties || 'menubar=yes,location=yes,width=700, height=400, scrollbars=yes, resizable= yes';
            windowProperties = windowProperties || 'menubar=yes,location=yes,width=' + (screen.availWidth - 15) + ', height=' + (screen.availHeight - 140) + ', scrollbars=yes, resizable= yes';
            windowTarget = windowTarget || "_blank";

            // Create a new window.
            var windowHandle = windowProperties ? window.open(url, windowTarget, windowProperties) : window.open(url, windowTarget);

            if (null === windowHandle || !windowHandle) {
                alert("You have a popup blocker enabled. Please allow popups for " + location.protocol + "//" + location.host);
            }
            else {
                if ("_blank" !== windowTarget) {
                    // Store the window handle for reuse if a handle was specified.
                    windowHandles[windowTarget] = windowHandle;
                    windowHandles[windowTarget].focus();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果有人在Mac上使用Chrome,我会很感激! (3认同)
  • 上面的解决方案:windowHandle.blur(); setTimeout(windowHandle.focus,0); 似乎不适用于Chrome 8.0.552.237.是否有任何其他解决方法可以使用setTimeout将焦点放在弹出窗口中? (2认同)
  • chrome开发人员在阅读此处的解决方案后似乎仍然无法解决这个问题:) (2认同)

Ric*_*haw 3

某人博客的建议是使用这个:

if (navigator.userAgent.indexOf('Chrome/') > 0) {
    if (window.detwin) {
        window.detwin.close();
        window.detwin = null;
    }
}
window.detwin = window.open(URL,  'windowname', '...');
window.detwin.focus();
Run Code Online (Sandbox Code Playgroud)

跟踪这个错误可能会有用。

  • @Rich,我在问题中提到了这一点。但这是一个糟糕的解决方案。如果弹出窗口中有您想要保存的数据怎么办?关闭窗口并重新打开 url 看起来很糟糕。 (4认同)
  • 上述解决方案:windowHandle.blur(); setTimeout(windowHandle.focus, 0); 似乎不适用于 Chrome 8.0.552.237。是否有其他解决方法可以使用 setTimeout 将焦点集中到弹出窗口? (2认同)