3

I am trying to write a code that will display a pop-up message when a site is first visited and I want it to never display again if I already see it.. Here's my code, I hope someone could help me to prevent the pop up from appearing when I refresh the page although it shouldn't.

<SCRIPT LANGUAGE="JavaScript">
<!--
    function GetCookie(cookie) {
        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 "here";
            i=document.cookie.indexOf(" ",i)+1;
            if (i==0) break;
        }
        return null;
    }
    var visit=GetCookie("cookie");
    if (visit==null){
        alert("Your Message Goes here and you only get to see it once!");
        var expire=new Date();
        expire=new Date(expire.getTime()+7776000000);
        document.cookie="cookie=here; expires="+expire;
    }
// -->
</SCRIPT>
4

1 回答 1

2

您的 getCookie 函数有问题。试试这个:

代码

function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    } else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}

这里的例子:http: //jsfiddle.net/TWB68/1/

于 2013-06-17T06:23:50.530 回答