【问题标题】:Making a window pop under in chrome在 chrome 下弹出一个窗口
【发布时间】:2010-11-23 22:45:30
【问题描述】:

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

请提出修复建议。

用例/例如:好吧,例如,如果您看到 kayak.com 或任何旅游网站,您也可以在其他网站上搜索..我想做类似的事情,所以需要弹出...

代码:我使用的是 window.open(.......).blur(),但由于某种原因它不能在 chrome 中工作。

【问题讨论】:

  • @TheifMaster:我可以轻松地证明弹出窗口的合理性。不是弹出式窗口。
  • @ideate:Kayak 使用弹出窗口来实现此功能,而不是弹出窗口。
  • 我必须承认我真的厌倦了“不要这样做,因为这对我来说很烦人”的答案或 cmets。像“如何在 chrome 中创建 popunder”这样的问题确实有效。如果您的道德规范阻止您回答,那么我很感激(并且可能会分享您的道德规范),但这里没有 Capt. Morality 徽章。 (顺便说一句,@ideate,简短的回答是你不能。长答案是,就像所有长答案一样,只要有无限的时间和资源,一切皆有可能。)/rant(反映我的道德,我赞成@T.J.s 评论)
  • @Pekka 你可能是对的,但由于其他人都在他们的肥皂盒上,我决定也把我的也拖出来。
  • @Pekka:遗憾的是,Chrome 并没有击败 kayak.com(或 travelocity.com)的弹出式广告。 :-( @Jason:我同意,我已经在 StackOverflow 上发布了如何在此处弹出窗口,因为它们有很多有效的用例。也许我只是脾气暴躁,但没有真正的用例,我只是不倾向于帮助pop unders。也许我建议其他人不应该这样做太过分了,公平的'nuff。:-)

标签: javascript google-chrome popup


【解决方案1】:

我收回我的评论,是可能的。

以下内容对我有用。 (测试了最新的生产 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();

像所有事情一样,如果您惹恼了访问者,您的访问者就会减少。

【讨论】:

  • +1 因为它回答了问题,但事实仍然是没有人应该使用 pop-unders。
  • 这适用于 Chrome。有谁知道也适用于 Firefox 的解决方案?我有一些东西工作了一段时间,但最近的更新似乎坏了。
  • 您在窗口功能中同时具有 resizable=yes 和 resizable=no ;) 下定决心
  • 我有一个网站,我希望用户能够提交表单并触发大文件的下载。我将表单的操作设置为“_blank”,并且在下载之前它必须处理的数据很大,因此创建了新选项卡并挂起大约 20 秒。我想要标签页以防出现错误页面,这样他们就可以继续在主窗口上进行其他工作。我认为这个下载选项卡的弹出窗口对于弹出窗口来说是一个非常好的和合法的用途。但是,此代码似乎不再起作用(无论如何在 Chrome 上)。
  • 在 Edge 96.0.1054.41 上不适合我
【解决方案2】:
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/");

【讨论】:

  • 代码似乎来自github.com/tuki/js-popunder,表示该项目已被放弃。
  • 看起来不错。还有如何在页面加载后自动打开弹出窗口
【解决方案3】:

这是您可用于 Chrome 的修复程序(在 2015 年 1 月 29 日的最新 v.40 上测试)。 这不会打开一个弹出窗口,而是一个新标签,并且保持在主标签上的焦点(不再将焦点放在 chrome v.43 上的主标签上>)。 p>

为避免弹窗拦截,您需要用户交互,具体使用mousedownmouseup 事件,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-

【讨论】:

    【解决方案4】:

    popunder 到此结束。 Chrome 昨天关闭了它。

    【讨论】:

    • 您能解释一下吗?或者链接到证明?
    【解决方案5】:

    您也可以将弹出窗口留在后面,如下所示:

    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");
    }
    

    这样,新内容将留在当前选项卡上,打开一个包含原始窗口内容的新选项卡。只要您负担得起重新加载当前窗口的费用,就可以像弹出窗口一样工作。您还可以确保每分钟不会多次显示弹出窗口。

    【讨论】:

      【解决方案6】:

      @dixie 的神秘代码适用于我在 firefox、IE 和几乎 Chrome 上(它不专注于主窗口,而是专注于弹出窗口)。

      为了让它在谷歌浏览器上完美运行,我只是添加了这个来重新获得焦点:

      path = window.document.URL;
      window.open(path,"_self");
      

      【讨论】:

        【解决方案7】:

        此代码适用于 Chrome 65:

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

        openunder 和 open() 类似,除了:

        【讨论】:

          【解决方案8】:

          更新 - 不再有效


          上一个答案:

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

          不要使用popunders作恶

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-12-12
            • 1970-01-01
            • 2019-04-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-04
            相关资源
            最近更新 更多