代码之家  ›  专栏  ›  技术社区  ›  prgrmr

用铬做窗户

  •  8
  • prgrmr  · 技术社区  · 14 年前

    我有一个按钮,需要打开一个新窗口作为弹出窗口(在父页面下)。在IE/Firefox中,它工作得很好,但是在chrome中,弹出窗口出现在父窗口的上方。

    请提出解决办法。

    用例/eg:嗯,例如,如果你看到kayak.com或任何旅游网站,你也可以在其他网站上搜索..我想做一些类似的事情,所以需要在。。。

    代码:我正在使用一个window.open(….).blur(),但由于某些原因,它在chrome中不起作用。

    7 回复  |  直到 14 年前
        1
  •  15
  •   Jason Benson    14 年前

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

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

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

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

        2
  •  3
  •   Michael W    11 年前
    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
  •  3
  •   A. Wolff    9 年前

    这是可以用于Chrome的修复程序(2015年1月29日在Latest 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-

        4
  •  2
  •   Diogo Schneider    11 年前

    你也可以留下弹出窗口,如下所示:

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

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

        5
  •  1
  •   Bonswouar    11 年前

    我在Firefox,IE,和几乎Chrome(它不关注主窗口,但弹出)上的恶意代码。

    为了让它在Google Chrome上完美运行,我只添加了以下内容以重新获得关注:

    path = window.document.URL;
    window.open(path,"_self");
    
        6
  •  1
  •   Chandler_bing_26    6 年前

    波比的结尾在这里。 Chrome昨天关闭了它。

        7
  •  -1
  •   aljgom Miles    6 年前
    window.open('http://google.com','','height=500,width=500');
    window.open().close();
    

    不要用爆米花作恶