【问题标题】:Javascript pop under window (Background window)Javascript弹出窗口(背景窗口)
【发布时间】:2014-12-26 10:44:24
【问题描述】:

我将使用 java 脚本在当前活动屏幕后面打开弹出窗口。 这是我的简单代码。

var win = window.open('http://yahoo.com',null,"height=400,width=600,status=yes,toolbar=no,scrollbars=yes,menubar=yes,location=yes");
win.blur();
this.window.focus();

但它似乎不适用于任何浏览器。它只是打开一个弹出窗口。我从 stackoverflow 引用了this question。但它不起作用。我的目标是在所有浏览器中创建一个背景窗口。请有人帮助我

【问题讨论】:

  • 你在哪个浏览器上测试这个?
  • 您真的要打开 yahoo 还是子窗口也是您的?
  • 你不能再重现这种行为,不是在所有浏览器中。人们对此感到厌倦......你最好的选择是github.com/tuki/js-popunder

标签: javascript jquery html popup


【解决方案1】:

你不能这样做。现代网络浏览器不允许网站从其他选项卡中窃取焦点(因为这对用户来说可能会变得非常混乱)。您唯一能做的就是提示您的用户在新选项卡中打开(这不会改变焦点)。或者您可以滥用警报将焦点重新返回到您的页面:

var win =   window.open('http://yahoo.com',null,"target=_blank,height=400,width=600,status=yes,toolbar=no,scrollbars=yes,menubar=yes,location=yes");
alert("Welcome back");

【讨论】:

  • 如何实现这一点。我需要创建背景窗口
  • @ling.s alert() 应该将焦点放在主窗口上,但 afaik,而不是跨浏览器
  • @ling.s 看起来您没有阅读所有 cmets 或不想了解发生了什么...
  • 我明白了.. 我在问是否还有其他方法?如果不可能,那么pop下开这么多网站?
  • @ling.s 大多数使用github.com/tuki/js-popunder 现在如果您有任何适用于 chrome 的链接,请提供?顺便说一句,如果你知道这种网站,为什么不检查来源?
【解决方案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/");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-25
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多