0

我正在尝试设置每 30 天(或任何其他设定时间段)将启动的 cookie,例如 Stackoverflow.com。

我在这里找到了一个我认为可能会有所帮助的答案。到目前为止,这是我的测试代码: HTML + Initiator

<html>
    <head>
        <!-- Load JQuery 1.8.3 and the repeatablePopup functions-->
        <script src="js/jquery-1.8.3.min.js"></script>
        <script src="js/repeatablePopup.js"></script>

        <script>
            jQuery(document).ready(function() {
                var visited = readCookie('visited');
                if (!visited || visited !== "true") {
                    createCookie('visited', "true", 7);
                } else {
                        window.open("http://stackoverflow.com");
                }
            });
        </script>
    </head>

    <body>
        <p>Setting Cookie</p>
    </body>

</html>

repeatablePopup.js 上的函数

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    document.write('Cookie is set');
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

编辑:现在的问题是每次刷新页面时都会植入 cookie,即使 cookie 存在。所以最终弹出窗口不起作用。

感谢您提供的任何帮助,沙哈尔

4

2 回答 2

0

试试这个:

jQuery(document).ready(function () {
    if (!readCookie('visited')) {
        window.open("http://stackoverflow.com");
        createCookie('visited', null, 30);
    }
});

当 cookie 不存在时,将打开弹出窗口。弹出窗口打开后,您设置 cookie。三十天后,cookie 将过期,一切将恢复正常并再次执行相同的操作。

于 2013-01-06T13:40:23.017 回答
0

尝试使用CoverPop.js,也许它可以帮助某人,有一些参数可以设置

CoverPop.start({
    coverId:             'CoverPop-cover',       // set default cover id
    expires:             30,                     // duration (in days) before it pops up again
    closeClassNoDefault: 'CoverPop-close',       // close if someone clicks an element with this class and prevent default action
    closeClassDefault:   'CoverPop-close-go',    // close if someone clicks an element with this class and continue default action
    cookieName:          '_CoverPop',            // to change the plugin cookie name
    onPopUpOpen:         function() {},          // on popup open callback function
    onPopUpClose:        function() {},          // on popup close callback function
    forceHash:           'splash',               // hash to append to url to force display of popup (e.g. http://yourdomain.com/#splash)
    delayHash:           'go',                   // hash to append to url to delay popup for 1 day (e.g. http://yourdomain.com/#go)
    closeOnEscape:       true                    // close if the user clicks escape
    delay:               0                       // set an optional delay (in milliseconds) before showing the popup
    hideAfter:           null                    // set an optional time (in milliseconds) to autohide
});
于 2015-05-06T08:36:48.167 回答