在chrome中创建一个窗口

问题描述 投票:8回答:7

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

请建议修复。

用例/例如:嗯,例如,如果你看到kayak.com或任何旅游网站,你也有能力搜索其他网站..我想做类似的事情,所以需要弹出下...

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

javascript google-chrome popup
7个回答
16
投票

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

以下对我有用。 (测试最新生产铬)

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();

像所有事情一样,如果你惹恼你的访客,你的访客就会减少。


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

3
投票

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

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下面的工作。您还确保弹出窗口不会每分钟显示多次。


3
投票

这是您可以用于Chrome的修复程序(在2015年1月29日的最新版本v.40上测试)。这不会打开一个窗口弹出窗口,而是一个新的选项卡和 保持主要标签集中 (不再关注chrome v.43上的主选项卡>)。

为避免弹出窗口阻止程序,您需要用户交互,特别是使用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-


1
投票

@dixie的神秘代码在firefox,I.E和几乎Chrome上为我工作(它不关注主窗口,而是关注弹出窗口)。

为了让它在Google Chrome上完美运行,我只需添加此功能即可重新获得关注:

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

1
投票

popunder的结尾就在这里。 Chrome昨天关闭了它。


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

不要使用popunders为邪恶

© www.soinside.com 2019 - 2024. All rights reserved.