设置弹出窗口每30天打开一次

时间:2013-01-06 10:40:33

标签: javascript jquery cookies

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

我找到了answer here我认为可能会有所帮助。 这是我到目前为止的测试代码: HTML +启动器

<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存在。 所以最终弹出窗口无效。

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

2 个答案:

答案 0 :(得分:0)

试试这个:

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

当cookie不存在时,弹出窗口将会打开。弹出窗口打开后,您可以设置cookie。三十天后,cookie将过期,一切都会恢复正常,并再次做同样的事情。

答案 1 :(得分: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
});