javascript - 使用 JS Cookie 添加 Cookie 到 Modal

标签 javascript cookies zurb-foundation

我正在使用 Foundation 和 JS Cookie 构建新闻通讯模式。我已经让模态正常工作,只需要正确获取 cookie 即可。

这是我的 JS...

$(document).ready(function() {
  if(Cookies.get('showed_newsletter', 'false' ) ) {
      setTimeout(function(){
        $('#myModal2').foundation('open'); 
        Cookies.set('showed_newsletter', 'true', { expires: 7 });
      },3000) // 3 seconds.
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.3/js.cookie.min.js"></script>

<div class="reveal small" id="myModal2" data-reveal data-animation-in="fade-in" data-animation-out="fade-out" data-reset-on-close="true" >
  <style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='embed-container'><iframe src='http://www.youtube.com/embed/QILiHiTD3uc' frameborder='0' allowfullscreen></iframe></div>
  <button class="close-button" data-close aria-label="Close reveal" type="button">
    <span aria-hidden="true">&times;</span>
  </button>
</div>

如有任何帮助,我们将不胜感激。

谢谢!

最佳答案

The doc表示 .get() 方法仅接受一个[可选]参数:cookie 名称

所以测试你的cookie是否像这样存在

if (!Cookies.get('showed_newsletter'))

如果没有,将返回未定义

Cookies.get('nothing'); // => undefined

$(document).ready(function() {
  if (!Cookies.get('showed_newsletter')) {
      setTimeout(function(){
        $('#myModal2').foundation('open'); 
        Cookies.set('showed_newsletter', 'true', { expires: 7 });
      },3000) // 3 seconds.
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.3/js.cookie.min.js"></script>

<div class="reveal small" id="myModal2" data-reveal data-animation-in="fade-in" data-animation-out="fade-out" data-reset-on-close="true" >
  <style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='embed-container'><iframe src='http://www.youtube.com/embed/QILiHiTD3uc' frameborder='0' allowfullscreen></iframe></div>
  <button class="close-button" data-close aria-label="Close reveal" type="button">
    <span aria-hidden="true">&times;</span>
  </button>
</div>

关于javascript - 使用 JS Cookie 添加 Cookie 到 Modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40349690/

相关文章:

javascript - Chrome 不会删除 session cookie

css - 为什么 CSS 框架不必要地使用 !important 标签?

javascript - 如何关闭我编写的这个 jQuery 模式?

alert - Zurb 基金会警报

javascript - 导航栏错误

javascript - Firebase 安全规则来自特定 Web url 的存储

javascript - 在异步 Jest 测试中是否需要完成?

android - Cookies & Webview - Android 中的 CookieSyncManager!

Javascript 不读取 Laravel 设置的 cookie

javascript - 使用 webdriverjs.io 和 jquery 的部分 ID 选择器