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

独自空忆成欢 提交于 2019-12-02 12:36:42

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/7996252/window-popups-how-to-get-window-blur-or-window-focus-to-work-in-firefox-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!