window.focus () funktioniert nicht in Google Chrome

Nur Wundern, wenn Google Chrome unterstützen window.focus() irgendwann. Wenn ich meine Unterstützung, meine ich. Die nennen es nicht scheitern, es funktioniert einfach nicht alles tun. Alle anderen großen Browser haben dieses problem nicht: FireFox, IE6-IE8 und Safari.

Ich habe eine client-side-Klasse für die Verwaltung von browser-Fenstern. Wenn ich zuerst ein Fenster erstellen und das Fenster kommt in den Vordergrund, aber die nachfolgenden versuche, den Fokus auf die Fenster funktionieren nicht.

Was ich sagen kann, dies scheint ein Sicherheitsfeature zu vermeiden, lästige pop-ups und es scheint nicht zu sein ein WebKit-Problem, wie es funktioniert in Safari.

Ich weiß, eine Idee, die jemand vorgezogen wurde, das Fenster zu schließen, öffnen Sie es erneut, aber dies ist eine schreckliche Lösung. Googeln zeigt, dass ich scheinbar nicht die einzige person, die frustriert mit diesem.

Und nur zu 100% klar, ich meine, neue Fenster, nicht tabs (Registerkarten können nicht fokussiert werden, von dem, was ich gelesen habe) und alle Fenster werden geöffnet, sind in der gleichen Domäne.

Irgendwelche Ideen, workarounds abgesehen von den schlechten, die ich oben erwähnt?

Es ist ein Fehler protokolliert, auf dem Chromium-Projekt zu diesem Thema wenden Sie sich hier. Dank für die Buchung, die Reichen.

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();
                }
            }
        }
    }
}
Kommentar zu dem Problem
Ich bin mit diesem problem für den IE 8-9.Irgendwelche Ideen? Kommentarautor: Bastardo

InformationsquelleAutor der Frage nickytonline | 2010-05-03

Schreibe einen Kommentar