使用JQuery Cookie每周显示一次Bootstrap模式
Displaying a Bootstrap Modal just once a Week with JQuery Cookie
我试图让我的模态只显示一周一次。对于用户访问我的主页的所有页面来说,这应该是有罪的。模态本身工作得很好。但每次我重新加载页面或转到另一个页面时,它都会出现。
提前感谢!
对于cookie,我使用如下:
https://github.com/js-cookie/js-cookie 下面是我的代码:// MODAL APPEAR AFTER 2 SECONDS
setTimeout(function() {
$('#newsletter-modal').modal();
}, 2000);
// MODAL COOKIE FOR 7 DAYS
$(function() {
if($.cookie('alreadyShow') === null) {
$.cookie('alreadyShow', true, {expires: 7});
$('#newsletter-modal').modal({
display: 'block'
});
}
});
<script type="text/javascript" src="/js/js.cookie.js"></script>
<div class="modal fade" id="newsletter-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h1 class="modal-title">Modal Headline</h1>
</div>
<div class="modal-body">
<p>Modal content goes here</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary newsletter-modal-close" data-dismiss="modal">Yes</button>
<button type="button" class="btn btn-default newsletter-modal-close" data-dismiss="modal">No</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
目前,无论cookie设置如何,每次加载页面时都会设置模式的超时时间。尝试将该块移动到设置cookie的函数中。这样,只有当一个cookie不存在时,它才会被调用。
// MODAL COOKIE FOR 7 DAYS
$(function() {
if($.cookie('alreadyShow') === null) {
// MODAL APPEAR AFTER 2 SECONDS
setTimeout(function() {
$('#newsletter-modal').modal();
}, 2000);
$.cookie('alreadyShow', true, {expires: 7});
$('#newsletter-modal').modal({
display: 'block'
});
}
});
相关文章:
- Javascript返回值只在循环中返回一次
- Jquery FadeIn FadeOut 只工作一次
- Javascript html每点击一次就会更改url
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- Rails操作只调用一次,但我在ajax中每秒钟都调用一次
- jQuery滚动功能只工作一次
- 刷新导致我的帖子“;张贴“;再一次
- Bootstrap Alert只显示一次
- Bootstrap Modal仅显示一次
- Bootstrap弹出窗口只出现一次
- Bootstrap popover destroy&recreate 仅每秒工作一次
- 与 Bootstrap 按钮关联的 jQuery 函数仅执行一次
- TinyMCE和Bootstrap Modals - 只工作一次
- Bootstrap DateTimePicker:一次打开一个日期时间选取器
- Jquery Twitter Bootstrap弹出工具提示,一次只允许一个弹出实例
- 使用JQuery Cookie每周显示一次Bootstrap模式
- Bootstrap 3菜单-两个折叠按钮-一次只有一个打开
- Modal中的Bootstrap手风琴只能工作一次
- Bootstrap可切换菜单单击一次后不工作
- Bootstrap/Javascript/JQuery使按钮一次改变一个颜色