【问题标题】:Load Magnific Popup once every 15 days for new user每 15 天为新用户加载一次 Magnific Popup
【发布时间】:2015-04-30 14:19:36
【问题描述】:

我有一个时事通讯注册表单,我希望每 15 天只加载(弹出)一次,否则它可能会有点烦人。我目前正在使用此 jquery 代码在页面加载时加载弹出表单。

<div id="test-popup" class="white-popup mfp-hide">
Popup Form
</div>

<script>
jQuery(window).load(function(){
jQuery.magnificPopup.open({
items: {src: '#test-popup'},type: 'inline'}, 0);
});
</script>

每次访问页面时加载表单时都可以正常工作,但我想限制这一点,以便新用户每 15 天看到一次。不确定 15 天是否是我想出的最佳做法?

【问题讨论】:

  • 我将 cookie 设置为每 15 天过期一次。如果它是假的/过期的。显示弹出窗口。 stackoverflow.com/questions/8103128/…
  • 你应该把它保存在数据库中
  • 我曾经使用这个带有颜色框的 cookie 脚本,但我想知道如何为 Magnific Popup 执行此操作?

标签: javascript jquery magnific-popup jquery-cookie


【解决方案1】:

can 使用 localStorage 来执行此操作。

$(window).on('load', function() {
  var now, lastDatePopupShowed;
  now = new Date();

  if (localStorage.getItem('lastDatePopupShowed') !== null) {
    lastDatePopupShowed = new Date(parseInt(localStorage.getItem('lastDatePopupShowed')));
  }

  if (((now - lastDatePopupShowed) >= (15 * 86400000)) || !lastDatePopupShowed) {
    $.magnificPopup.open({
      items: { src: '#test-popup' },
      type: 'inline'
    }, 0);

    localStorage.setItem('lastDatePopupShowed', now);
  }
});
<div id="test-popup" class="white-popup mfp-hide">
  Popup Form
</div>

您可以在此处查看一个工作示例:http://codepen.io/caio/pen/Qwxarw

【讨论】:

  • 'localStorage' 似乎是一个非常有用的方法。
【解决方案2】:

要让您的弹出窗口每 15 天为用户打开一次,您可能需要设置一个每 15 天过期的 cookie。在您的页面上,检查 cookie 是否已过期,如果是,请显示您的表单并重置您的 cookie。

this thread,您可以找到快速开始使用 cookie 的材料。

这将适用于每台计算机的每个浏览器,即如果用户在其他浏览器中打开您的页面,它将再次加载您的弹出窗口。

【讨论】:

    【解决方案3】:

    创建和读取 cookie 的函数:

    function createCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
       else var expires = "";
       document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
           if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    
        }
        return null;
    }
    

    创建 15 天的 cookie:

    createCookie('run_popup',true,15);
    

    检查过去的 15 天

    if(!readCookie('run_popup'))
    ... code for run popup...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多