【问题标题】:jQuery cookie on page load fancybox popup页面加载fancybox弹出窗口上的jQuery cookie
【发布时间】:2020-04-01 04:01:47
【问题描述】:

我在jQuery中使用fancybox弹出窗口,

如何在每次用户进入我的网站时显示它?最好在每个会话中显示一次……

这是我如何实现/设置 cookie 的示例

http://www.sohtanaka.com/web-design/examples/modal-window/index2.htm

【问题讨论】:

    标签: jquery cookies fancybox


    【解决方案1】:

    fancybox 打开后创建一个 cookie:

    'onComplete' : function() {
            // set cookie
        }
    

    但在打开fancybox 之前,请务必检查cookie 是否存在。不要设置“过期”值,这将在会话结束时强制删除 cookie。

    编辑:

    只是扩展您发布的代码:

    $(document).ready(function(){
    
        var check_cookie = $.cookie('the_cookie');
    
        if(check_cookie == null){
    
            $.fancybox('<h2>Some content</h2>', {
                'onComplete' : function() {
                        $.cookie('the_cookie', 'the_value');
                }
            });
        }
    });
    

    如果页面加载并且脚本找不到 cookie,fancybox 就会打开。如果页面加载并且 cookie 存在,则花式框不会打开。

    这里有一些关于opening fancybox when the page loads 的更多信息。

    【讨论】:

    • 感谢 digvyswift 但我在 js 方面并不聪明,你能详细写一下吗?还是我应该把它放在后面
    • 感谢 frnd !我在 js 中不太聪明,我从某个地方得到了这段代码并放在 // $(document).ready(function(){ var check_cookie = $.cookie('the_cookie'); if(check_cookie == null){ $.cookie('the_cookie', 'the_value'); //fire your fancybox here } }); 之后但不能工作 news.shreshthbharat.in/fancy-box_onload/index-2.html
    【解决方案2】:

    您需要使用 cookie 插件才能工作 - https://github.com/carhartl/jquery-cookie

    然后使用 Digbyswift 发布的代码...效果很好!

    $(document).ready(function(){
    
        var check_cookie = $.cookie('the_cookie');
    
        if(check_cookie == null){
    
            $.fancybox('<h2>Some content</h2>', {
                'onComplete' : function() {
                        $.cookie('the_cookie', 'the_value');
                }
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多