Pop-Under:每个会话和Onload只有一次

时间:2014-12-18 13:24:11

标签: javascript popup

我正在谷歌搜索,但我找不到一个好的解决方案。我在脚本下搜索一个pop,打开一个新的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);?>

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我设法修改了我在网上找到的一个代码示例(类似于你的代码样本),以便自动在文档加载时打开Pop-Under。需要结合使用jQuery来实现它,但是我删除了cookie代码段,因此&#34;每次会话一次&#34;事情是行不通的。

以下是代码:

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

注意我在&#34; $(document).ready(function(e){...}&#34;中使用了jQuery。这会在文档加载时触发事件。

然后只需添加&#34; makePopunder(&#34; https://www.myurl.com/&#34;);&#34;在html文档正文中的脚本标记内。

上面的原始脚本每次会话都有&#34;一次&#34;使用Cookie的功能,欢迎您使用我的修改编辑它并保留Cookie的原始代码: http://community.sitepoint.com/t/popunder-that-works-well-for-all-browser/29741

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