【问题标题】:Using a cookie and jQuery BlockUI使用 cookie 和 jQuery BlockUI
【发布时间】:2013-11-27 21:43:47
【问题描述】:

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

代码如下:

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

【问题讨论】:

    标签: jquery cookies popup blockui jquery-blockui


    【解决方案1】:

    您不需要使用 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");
    

    【讨论】:

      【解决方案2】:

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

      jQuery(function($) {
          if (!$.cookie('blockuicookie')) {
              // blockUI scripts goes here.
              $.cookie('blockuicookie', true, { "expires": 30, "path": "/" });
              setTimeout($.unblockUI, 30500);
          }
      });
      

      【讨论】:

      • 谢谢。这样可行。唯一的问题,设置cookie后,弹出的图片不显示,显示在页面的最底部。
      • 我想通了。我设置了“代码”#displaybox {position:fixed;width:556px;显示:无;}
      • 如果用户解决了您的问题,请不要忘记将其标记为已解决,以供将来的访问者使用。因此,如果有人遇到类似问题并来到这里,他们可以轻松找到相同的解决方案。
      猜你喜欢
      • 2012-01-31
      • 2012-05-03
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多