【问题标题】:Pop-Under : only one time per session and OnloadPop-Under:每个会话只有一次和 Onload
【发布时间】:2014-12-18 13:24:10
【问题描述】:

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

你能帮帮我吗?

【问题讨论】:

    标签: javascript popup


    【解决方案1】:

    我设法修改了我在网上找到的一个代码示例(与您的类似),以便在加载文档时自动打开一个 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 的。这会在文档加载时触发事件。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 2021-07-16
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多