3

我想在主页上显示一个弹出窗口,但只有一次。如果有人转到其他页面然后又回到主页,我不想再次显示它。

这是代码:

<script type="text/javascript" language="javascript">
    $(document).ready(function(){
    $("#displaybox").hide();
    message: $('#displaybox'),
        css: {
            top:  ($(window).height() - 391) /2 + 'px',
            left: ($(window).width() - 556) /2 + 'px',
            width: '556px'
           }
    });
    $('.displayboxclose').attr('title','Click to close').click($.unblockUI);

    setTimeout($.unblockUI, 30500);

});
</script>

有人可以帮忙插入 cookie 代码吗?

我正在使用 jQuery BlockUI 插件和 jquery-cookie 插件

4

2 回答 2

2

像这样的东西可能对你有用;

jQuery(function($) {
    if (!$.cookie('blockuicookie')) {
        // blockUI scripts goes here.
        $.cookie('blockuicookie', true, { "expires": 30, "path": "/" });
        setTimeout($.unblockUI, 30500);
    }
});
于 2013-11-14T18:19:35.923 回答
0

您不需要使用 jquery cookie 插件:

function setCookie(name, value, expires, path, domain, secure) {
    var caution = false;
    var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
    if (!caution || (name + "=" + escape(value)).length <= 4000)
        document.cookie = curCookie;
        else
            if (confirm("Cookie exceeds 4KB and will be cut!"))
                document.cookie = curCookie;
}

//name - name of the cookie
//* return string containing value
//of specified cookie or null if cookie
//does not exist
function getCookie(name) {
    var prefix = name + "=";
    var cookieStartIndex = document.cookie.indexOf(prefix);
    if (cookieStartIndex == -1)
        return null;
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
                prefix.length);
                if (cookieEndIndex == -1)
                    cookieEndIndex = document.cookie.length;
                    return unescape(document.cookie.substring(cookieStartIndex +
                            prefix.length,
                            cookieEndIndex));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

要以这种方式设置 cookie 调用:

setCookie("dateModified","11/14/2013");

要获取 cookie:

var lastDate = getCookie("dateModified");
于 2013-11-14T18:17:11.617 回答