在Chrome中使窗口弹出并处于最下层

11
我有一个按钮需要将新窗口作为弹出窗口(在父页面下)打开。 在IE / Firefox中可以正常工作,但在Chrome中弹出窗口会出现在父窗口上方。

请提供解决方法。

用例/例如:比如在kayak.com或任何旅游网站上,您也可以在其他网站上进行搜索..我想做类似的事情,因此需要弹出窗口...

代码:我正在使用window.open(.......).blur(),但由于某种原因在Chrome中无法正常工作。


2
@TheifMaster:我可以轻松地为弹出窗口辩护,但不能为弹出窗口下方的广告辩护。 - T.J. Crowder
3
Kayak使用弹出窗口实现这个功能,而不是弹出式广告。 - Robert Harvey
30
我必须承认,当别人回答“你这样做很烦人”时,我真的很厌倦。像“如何在Chrome中创建弹出窗口”这样的问题确实是有效的。如果你的职业道德准则阻止你回答,那么我感激(并可能分享你的职业道德),但这里没有道德警长徽章。(顺便说一下@ideate,简短的答案是你不能。长的答案就像所有长的答案一样,只要有无限的时间和资源,任何事情都是可能的。)/发牢骚(基于我的道德观,我点了赞@T.J.的评论) - Jason Benson
2
@Pekka,你可能是对的,但既然其他人都在高谈阔论,我也决定把我的拿出来。 - Jason Benson
1
@Pekka:遗憾的是,Chrome无法阻止kayak.com(或travelocity.com)的弹出式广告。 :-( @Jason: 我同意,并在StackOverflow上发布了如何进行弹出窗口的内容,因为它们有许多有效的用例。也许我只是有些脾气不好,但在没有真正的用例的情况下,我就不想帮助弹出式广告了。也许我过于苛刻,建议别人不要这样做,公平就行。 :-) - T.J. Crowder
显示剩余9条评论
8个回答

20

我撤回我的评论,这是可能的。

以下内容对我有效。(在最新的生产版Chrome上测试过)

var url = "yourURL.html";
window.open(url, "s", "width= 640, height= 480, left=0, top=0, resizable=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no").blur();
window.focus();

就像所有事情一样,如果你惹恼了你的访问者,那么你将会拥有更少的访问者。


7
+1是因为它回答了问题,然而事实仍然存在:没有人应该使用弹出窗口广告。 - casablanca
1
那在Chrome中可以运行。有人知道一个在Firefox中也能运行的解决方案吗?我之前有一个一直可以用的方法,但最近的更新好像出了问题。 - alexp
8
您的窗口功能中同时包含了 resizable=yes 和 resizable=no,需要做出决定哦 ;) - Jesper
2
我有一个网站,我希望用户能够提交表单并触发大文件的下载。我将表单的操作设置为“_blank”,并且它必须在下载之前处理的数据非常庞大,因此新标签页被创建并挂起了大约20秒。我想要这个标签页以防出现错误页面,并且他们可以在主窗口上继续做其他工作。我认为弹出式窗口对于这个下载标签页来说是一个完全良好和合法的使用方式。然而,这段代码似乎不再起作用(至少在Chrome上)。 - bozdoz
在 Edge 96.0.1054.41 上对我不起作用。 - cskwg

4
弹出窗口(popunder)的终结已经到来了。 Chrome昨天关闭了它。

1
请您能否详细解释一下?或者提供证明链接? - Scott

3
这是可用于Chrome的修复方法(在2015年1月29日测试的最新版本v.40)。 这不会打开窗口弹出框,而是打开一个新标签页并取消主标签页的关注(Chrome v.43后不再保持主标签页关注)。 为了避免弹出式阻止程序,您需要用户交互,特别是使用mousedown或mouseup事件,click将引发弹出式阻止程序警告。
document.addEventListener("mousedown", tabUnder);

function tabUnder() {
    var a = document.createElement("a"),
        e = document.createEvent("MouseEvents");
    a.href = "http://testit.com"; //the URL of 'popup' tab
    e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
    a.dispatchEvent(e);
    document.removeEventListener("mousedown", tabUnder);
}

-jsFiddle-


3
function makePopunder(pUrl) {
    var _parent = (top != self && typeof (top["document"]["location"].toString()) === "string") ? top : self;
    var mypopunder = null;
    var pName = (Math["floor"]((Math["random"]() * 1000) + 1));
    var pWidth = window["innerWidth"];
    var pHeight = window["innerHeight"];
    var pPosX = window["screenX"];
    var pPosY = window["screenY"];
    var pWait = 3600;
    pWait = (pWait * 1000);
    var pCap = 50000;
    var todayPops = 0;
    var cookie = "_.mypopunder";
    var browser = function () {
        var n = navigator["userAgent"]["toLowerCase"]();
        var b = {
            webkit: /webkit/ ["test"](n),
            mozilla: (/mozilla/ ["test"](n)) && (!/(compatible|webkit)/ ["test"](n)),
            chrome: /chrome/ ["test"](n),
            msie: (/msie/ ["test"](n)) && (!/opera/ ["test"](n)),
            firefox: /firefox/ ["test"](n),
            safari: (/safari/ ["test"](n) && !(/chrome/ ["test"](n))),
            opera: /opera/ ["test"](n)
        };
        b["version"] = (b["safari"]) ? (n["match"](/.+(?:ri)[\/: ]([\d.]+)/) || [])[1] : (n["match"](/.+(?:ox|me|ra|ie)[\/: ]([\d.]+)/) || [])[1];
        return b;
    }();

    function isCapped() {
        try {
            todayPops = Math["floor"](document["cookie"]["split"](cookie + "Cap=")[1]["split"](";")[0]);
        } catch (err) {};
        return (pCap <= todayPops || document["cookie"]["indexOf"](cookie + "=") !== -1);
    };

    function doPopunder(pUrl, pName, pWidth, pHeight, pPosX, pPosY) {
        if (isCapped()) {
            return;
        };
        var sOptions = "toolbar=no,scrollbars=yes,location=yes,statusbar=yes,menubar=no,resizable=1,width=" + pWidth.toString() + ",height=" + pHeight.toString() + ",screenX=" + pPosX + ",screenY=" + pPosY;
        document["onclick"] = function (e) {
            if (isCapped() || window["pop_clicked"] == 1 || pop_isRightButtonClicked(e)) {
                //return;
            };
            window["pop_clicked"] = 1;
            mypopunder = _parent["window"]["open"](pUrl, pName, sOptions);
            if (mypopunder) {
                var now = new Date();
                document["cookie"] = cookie + "=1;expires=" + new Date(now["setTime"](now["getTime"]() + pWait))["toGMTString"]() + ";path=/";
                now = new Date();
                document["cookie"] = cookie + "Cap=" + (todayPops + 1) + ";expires=" + new Date(now["setTime"](now["getTime"]() + (84600 * 1000)))["toGMTString"]() + ";path=/";
                pop2under();
            };
        };
    };

    function pop2under() {
        try {
            mypopunder["blur"]();
            mypopunder["opener"]["window"]["focus"]();
            window["self"]["window"]["blur"]();
            window["focus"]();
            if (browser["firefox"]) {
                openCloseWindow();
            };
            if (browser["webkit"]) {
                openCloseTab();
            };
        } catch (e) {};
    };

    function openCloseWindow() {
        var ghost = window["open"]("about:blank");
        ghost["focus"]();
        ghost["close"]();
    };

    function openCloseTab() {
        var ghost = document["createElement"]("a");
        ghost["href"] = "about:blank";
        ghost["target"] = "PopHelper";
        document["getElementsByTagName"]("body")[0]["appendChild"](ghost);
        ghost["parentNode"]["removeChild"](ghost);
        var clk = document["createEvent"]("MouseEvents");
        clk["initMouseEvent"]("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
        ghost["dispatchEvent"](clk);
        window["open"]("about:blank", "PopHelper")["close"]();
    };

    function pop_isRightButtonClicked(e) {
        var rightclick = false;
        e = e || window["event"];
        if (e["which"]) {
            rightclick = (e["which"] == 3);
        } else {
            if (e["button"]) {
                rightclick = (e["button"] == 2);
            };
        };
        return rightclick;
    };
    if (isCapped()) {
        return;
    } else {
        doPopunder(pUrl, pName, pWidth, pHeight, pPosX, pPosY);
    };
}

makePopunder("http://www.yourdomain.com/");

4
代码似乎来自 https://github.com/tuki/js-popunder,该项目已经被抛弃。 - Bibek Shrestha
看起来不错。另外,如何在页面加载后自动打开弹出窗口? - chandrashekar

2
你可以像这样将弹出窗口留在后面:
var MINUTE_MILLISECONDS = 60000;
var now = new Date().getTime();

if (!localStorage.t || now > parseInt(localStorage.t) + MINUTE_MILLISECONDS) {
    var date = new Date();
    localStorage.t = now;
    window.location.href = "http://dgsprb.blogspot.com/";
    window.open(window.document.URL, "_blank");
}

这样,新内容就会留在当前标签页上,同时用原始窗口内容打开一个新的标签页。它的工作方式与 pop under 类似,只要您能够重新加载当前窗口。您还可以确保弹出窗口不会在一分钟内显示多次。


1
@dixie 的神秘代码在 Firefox、IE 和几乎所有的 Chrome 浏览器上都能工作(它不会聚焦在主窗口而是弹出窗口上)。
为了让它完美地在 Google Chrome 上运行,我只需添加以下内容来重新获取焦点:
path = window.document.URL;
window.open(path,"_self");

0

更新 - 不再起作用了


之前的回答:

window.open('http://google.com','','height=500,width=500');
window.open().close();

不要将弹出广告用于恶意行为


0

这段代码适用于Chrome 65及以下版本:

function just_open() {
    postMessage([...arguments]);
}
window.onmessage = function({data}){
    return open(...data);
}
function openunder() {
    just_open([...arguments]);
    window.open().close();
}

其中openunder与open()类似,但有以下不同:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接