弹出式:每个会话和加载仅一次

Pop-Under : only one time per session and Onload

本文关键字:一次 加载 会话 弹出式      更新时间:2023-09-26

我在谷歌上搜索,但我找不到一个好的解决方案。我正在脚本下搜索一个弹出窗口,该弹出窗口打开一个新的 url 窗口,但每次看到只有一次。我找到了很多脚本,但打开一个新窗口只是为了 onclick...

<script>
function jsPopunder(sUrl, sConfig) {
    sConfig = (sConfig || {});
    var sName   = (sConfig.name   || Math.floor((Math.random()*1000)+1));
    var sWidth  = (sConfig.width  || window.innerWidth);
    var sHeight = (sConfig.height || window.innerHeight);
    var sPosX = (typeof(sConfig.left)!= 'undefined') ? sConfig.left.toString() : window.screenX;
    var sPosY = (typeof(sConfig.top) != 'undefined') ? sConfig.top.toString()  : window.screenY;
    /* capping */
    var sWait = (sConfig.wait || 3600); sWait = (sWait*1000);
    var sCap  = (sConfig.cap  || 2);
    /* cookie stuff */
    var popsToday = 0;
    var cookie = (sConfig.cookie || '__.popunder');
    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 {
            popsToday = Math.floor(document.cookie.split(cookie+'Cap=')[1].split(';')[0]);
        } catch(err){}
        return (sCap<=popsToday || document.cookie.indexOf(cookie+'=') !== -1);
    }
    function openIt(sUrl, sName, sOptions) {
        if (isCapped()) return;
        var _parent = (top != self && typeof(top.document.location.toString())==='string') ? top : self;
        var popunder = _parent.window.open(sUrl, sName, sOptions);
        if (popunder) {
            popunder.blur();
            setTimeout(function() {
                document.onclick = function() { return; };
                document.onmousedown = function() { return; };
            }, 1000);
            var now = new Date();
            document.cookie = cookie+'=1;expires='+ new Date(now.setTime(now.getTime()+sWait)).toGMTString() +';path=/';
            now = new Date();
            document.cookie = cookie+'Cap='+(popsToday+1)+';expires='+ new Date(now.setTime(now.getTime()+(84600*1000))).toGMTString() +';path=/';
            window.focus();
            try{ opener.window.focus(); }catch(err){}
        }
        return popunder;
    }
    function popunder(sUrl, sName, sWidth, sHeight, sPosX, sPosY) {
        if (isCapped()) return;
        var sOptions = 'toolbar=no,scrollbars=yes,location=yes,statusbar=yes,menubar=no,resizable=1,width='+sWidth.toString()+',height='+sHeight.toString()+',screenX='+sPosX+',screenY='+sPosY;
        if (browser.webkit) {
            document.onmousedown = function () {
                openIt(sUrl, sName, sOptions);
            };
            document.onbeforeunload = function() {
                window.open("about:blank").close();
            };
        } else {
            document.onbeforeunload = function() {
                var popunder = openIt(sUrl, sName, sOptions);
                if (popunder) {
                    if (!browser.msie) {
                        popunder.params = { url: sUrl };
                        (function(e) {
                            with (e) {
                                if (typeof window.mozPaintCount != 'undefined' || typeof navigator.webkitGetUserMedia === "function") {
                                    try {
                                        var poltergeist = document.createElement('a');
                                        poltergeist.href = "javascript:window.open('about:blank').close();document.body.removeChild(poltergeist)";
                                        document.body.appendChild(poltergeist).click();
                                    }catch(err){}
                                }
                            }
                        })(popunder);
                    }
                }
            };
        }
    }
    // abort?
    if (isCapped()) {
        return;
    } else {
        popunder(sUrl, sName, sWidth, sHeight, sPosX, sPosY);
    }
}
jsPopunder('http://www.google.com');
</script>
<?php mysql_close($base);?>

你可以帮我吗?

我设法修改了我在网上找到的代码示例之一(类似于您的代码示例(,以便在文档加载时自动打开一个 Pop-Under。需要结合jQuery来实现它,但是我删除了cookie代码段,因此"每个会话一次"的事情将不起作用。

代码如下:

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 doPopunder(pUrl, pName, pWidth, pHeight, pPosX, pPosY) {
    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 ).ready(function (e) {
        window["pop_clicked"] = 1;
        mypopunder = _parent["window"]["open"](pUrl, pName, sOptions);
        if (mypopunder) {
            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;
};
doPopunder(pUrl, pName, pWidth, pHeight, pPosX, pPosY);}

请注意我在"$( document (.ready(function (e( {...}"中使用jQuery的。这将在文档加载时触发事件。

然后只需在 html 文档正文的脚本标签中添加"makePopunder("https://www.myurl.com/"(;"。

上面的原始脚本具有使用 cookie 的"每个会话一次"功能,因此欢迎您使用我的修改对其进行编辑并保留 cookie 的原始代码:http://community.sitepoint.com/t/popunder-that-works-well-for-all-browser/29741

适用于所有浏览器(桌面和移动设备(:Firefox,Chrome,Safari等。