window.focus()在Google Chrome中无法使用 [英] window.focus() not working in Google Chrome

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

问题描述

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



我有一个客户端类管理浏览器窗口。当我第一次创建一个窗口时,窗口变成了焦点,但随后尝试将焦点放在窗口中不起作用。



从我可以知道,这似乎是一个安全功能,以避免烦人的弹出窗口,它似乎不是一个WebKit问题,因为它在Safari中工作。



我知道有人提出的一个想法是关闭窗口然后重新打开它,但这是一个可怕的解决方案。 Googling表明,我不是唯一的沮丧的人。



只是为了100%清除,我的意思是新的窗口,不是标签(标签不能集中于我读过的内容),并且所有打开的窗口都在同一个域中。



除了上面提到的坏的方法, p>

Chromium项目中记录了一个错误,请检查 here 。感谢您发布 Rich

  MyCompany = {UI:{}}; //如果你想测试代码,放在这里。我在代码中的其他地方创建这些命名空间。 

MyCompany.UI.Window = new function(){
//私有字段
var that = this;
var windowHandles = {};

//公共成员
this.windowExists = function(windowTarget){
return windowTarget&& windowHandles [windowTarget]&& !windowHandles [windowTarget] .closed;
}

this.open = function(url,windowTarget,windowProperties){
//看看我们是否有一个窗口句柄,如果它关闭或不关闭。
if(that.windowExists(windowTarget)){

//我们还有我们的窗口对象,所以让我们检查这些URL是否与我们要加载的URL相同。
var currentLocation = windowHandles [windowTarget] .location;

if(

/^http(?:s?):/.test(url)&& currentLocation.href!== url

||

//此检查是必需的,因为URL可能是相同的,但是绝对的,
//例如/Default.aspx ...而不是http ://localhost/Default.aspx ...
!/ ^ http(?:s?):/。test(url)&&
(currentLocation.pathname + currentLocation.search + currentLocation .hash)!== url

){
//不是相同的URL,所以加载新的。
windowHandles [windowTarget] .location = url;
}

//将焦点放在窗口中。这工作在IE 6/7/8,FireFox,Safari,但不是Chrome。
//在Chrome中它是第一次工作,但后续的焦点尝试失败,。我相信这是Chrome中的安全功能,可避免烦人的弹出式窗口。
windowHandles [windowTarget] .focus();
}
else
{
//需要这样做,使得标签浏览器(几乎所有浏览器,除了IE6)实际上打开一个新窗口
//相反到标签。通过指定至少一个窗口属性,我们保证创建一个新窗口,而不是
//的标签页。
windowProperties = windowProperties || 'menubar = yes,location = yes,width = 700,height = 400,scrollbars = yes,resizable = yes';
windowTarget = windowTarget || _空白;

//创建一个新窗口。
var windowHandle = windowProperties? window.open(url,windowTarget,windowProperties):window.open(url,windowTarget);

if(null === windowHandle){
alert(您启用了弹出式窗口拦截器,请允许弹出+ location.protocol +//+ location.host) ;
}
else {
if(_blank!== windowTarget){
//如果指定了句柄,存储窗口句柄以供重用。
windowHandles [windowTarget] = windowHandle;
windowHandles [windowTarget] .focus();
}
}
}
}
}


解决方案

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


Just wondering if Google Chrome is going to support window.focus() at some point. When I mean support, I mean have it work. The call to it doesn't fail, it just doesn't do anything. All other major browsers do not have this problem: FireFox, IE6-IE8 and Safari.

I have a client-side class for managing browser windows. When I first create a window the window comes into focus, but subsequent attempts to bring focus to the window do not work.

From what I can tell, this appears to be a security feature to avoid annoying pop-ups and it does not appear to be a WebKit issue as it works in Safari.

I know one idea someone brought forward was to close the window then reopen it, but this is a horrible solution. Googling shows that I do not appear to be the only person frustrated with this.

And just to be 100% clear, I mean new windows, not tabs (tabs cannot be focused from what I've read) and all the windows being opened are in the same domain.

Any ideas, workarounds aside from the bad one I mention above?

There is a bug logged on the Chromium project about this, check it out here. Thanks for posting that 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();
                }
            }
        }
    }
}

解决方案

Still the same in version 14.0.835.202 m on Windows 7; found another workaround, not more elegant but at least will avoid losing data on the page: show an alert in the window you want to focus.

这篇关于window.focus()在Google Chrome中无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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