每30天弹出一次

时间:2014-01-11 19:14:20

标签: javascript session cookies

我完全是JS和cookie的新手。我在线获取此代码并尝试将其更改为30天(设置为365)但它可能会重置每个页面的cookie,如果我转到其他页面甚至返回到原始页面,则会弹出弹出窗口。只有我在代码中更改的内容是到期日为30天,加载延迟为30秒。 似乎要么每次我移动到其他页面时重置cookie,或者其他一些我还不了解的问题:)。我想知道是否有一些更有效的方法,而不是把代码放在每个HTML文章页面。比如在标题或东西中设置cookie并使用body onload调用。 这是代码:

<SCRIPT language=JavaScript>
    <!--
    var expDays = 30; // number of days the cookie should last

    function GetCookie (name) { 
        var arg = name + "="; 
        var alen = arg.length; 
        var clen = document.cookie.length; 
        var i = 0; 
        while (i < clen) { 
            var j = i + alen; 
            if (document.cookie.substring(i, j) == arg) 
                return getCookieVal (j); 
            i = document.cookie.indexOf(" ", i) + 1; 
            if (i == 0) break; 
        } 
        return null;
    }

    function SetCookie (name, value,expires) {
        var argv = SetCookie.arguments; 
        var argc = SetCookie.arguments.length; 
        var expires = (argc > 2) ? argv[2] : null; 
        var path = (argc > 3) ? argv[3] : null; 
        var domain = (argc > 4) ? argv[4] : null; 
        var secure = (argc > 5) ? argv[5] : false; 
        document.cookie = name + "=" + escape (value) + 
            ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
            ((path == null) ? "" : ("; path=" + path)) + 
            ((domain == null) ? "" : ("; domain=" + domain)) + 
            ((secure == true) ? "; secure" : "");
    }

    function DeleteCookie (name) { 
        var exp = new Date(); 
        exp.setTime (exp.getTime() - 1); 
        var cval = GetCookie (name); 
        document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }    
    var exp = new Date(); 
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

    function getCookieVal(offset) {
        var endstr = document.cookie.indexOf (";", offset);
        if (endstr == -1)
            endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }    

    function checkCount() {
        var count = GetCookie('poponce');
        if (count == null) {
            count++;
            SetCookie('poponce', count, exp);

            // Action to take
            dothis()
        }
    }
    setTimeout(function dothis(){
        var windowprops = "left=300,top=240,width=600,height=400,location=no,toolbar=no,menubar=no,scrollbars=no";
        window.open("/subscribepopup.html", "", windowprops); // Use to load a page
    }, 30000);
    // -->
</SCRIPT> 

<body OnLoad="checkCount()"> 

0 个答案:

没有答案