为首次访问者弹出消息

时间:2013-06-17 06:01:39

标签: javascript

我正在尝试编写一个代码,当首次访问网站时会显示弹出消息,如果我已经看到它,我希望它再也不会显示..这是我的代码,我希望有人可以帮助我当我刷新页面时,防止弹出窗口出现,尽管它不应该出现。

<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>

1 个答案:

答案 0 :(得分:2)

你的getCookie功能有问题。试试这个:

<强> CODE

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/

相关问题