简体   繁体   中英

Window popups - how to get window.blur() or window.focus() to work in FireFox 4?

I'm aware that FF4 doesn't allow the use of window.blur() unless "Raise or lower window" setting is enabled in the FF configuration. It simple ignores the event.

I'm aware that some site still manage to open a pop-up window and keep focus on your current window, even when this setting is switched off. How do they achieve this?

Additionally, I don't understand why .blur() and .focus() doesn't work when both pages reside on the same domain. According to http://support.mozilla.com/en-US/questions/806756#answer-167267 this should work.

This works for me in Firefox and Chrome, in default settings ( JSFiddle ):

function popUnder(url, width, height) {
    var popUnderWin, nav = navigator.userAgent,
        isGecko = /rv:[2-9]/.exec(nav),
        hackString;

    hackString = nav.indexOf('Chrome') > -1 ? "scrollbar=yes" : "toolbar=0,statusbar=1,resizable=1,scrollbars=0,menubar=0,location=1,directories=0";

    popUnderWin = window.open("about:blank", "title", hackString + ",height=" + height + ",width=" + width);

    if (isGecko) {
        popUnderWin.window.open("about:blank").close();
    }

    popUnderWin.document.location.href = url;

    setTimeout(window.focus);
    window.focus();
    popUnderWin.blur();
}

document.getElementById("asd").addEventListener("click", function() {
    popUnder("http://www.google.com", 1024, 768);
}, false);
<div id="asd">click here</div>

I didn't manage to get it work without the hacky extra parameters to window.open , so there is something to them.

http://support.mozilla.com/en-US/questions/806756#answer-167267

They say that it isn't possible unless everybody goes to about:config and sets dom.disable_window_flip to false .

I am not aware of any code that bypasses this restriction but I think the other websites use something other than window.blur() and window.focus()

There is a similar article here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM