"Modal with jQuery Cookie"
Bootstrap 3.1.0 Snippet by coskuntekin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!--
Referances
jQuery Cookie : https://github.com/carhartl/jquery-cookie
Modal : http://getbootstrap.com/javascript/#modals
-->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<div class="container">
<div class="row">
<div class='modal fade' id='myModal'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class='modal-title'>
<strong>MODAL WITH JQUERY COOKIE</strong>
</h4>
</div>
<!-- / modal-header -->
<div class='modal-body'>
<img class="img-responsive" src="http://placehold.it/600x350&text=MODAL" />
</div>
<!-- / modal-body -->
<div class='modal-footer'>
<div class="checkbox pull-right">
<label>
<input class='modal-check' name='modal-check' type="checkbox"> Don't Show
</label>
</div>
<!--/ checkbox -->
</div>
<!--/ modal-footer -->
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$(document).ready(function(){
//Referances
//jQuery Cookie : https://github.com/carhartl/jquery-cookie
//Modal : http://getbootstrap.com/javascript/#modals
var my_cookie = $.cookie($('.modal-check').attr('name'));
if (my_cookie && my_cookie == "true") {
$(this).prop('checked', my_cookie);
console.log('checked checkbox');
}
else{
$('#myModal').modal('show');
console.log('uncheck checkbox');
}
$(".modal-check").change(function() {
$.cookie($(this).attr("name"), $(this).prop('checked'), {
path: '/',
expires: 1
});
});
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related: See More


Questions / Comments:

Navazkhan () - 9 years ago - Reply 0


Thank you @coskuntekin:disqus for this beautiful snippet!
Question: how do you set the checkbox to 'always' checked so that when we decide to put 2 buttons with different options then the checkbox must always checked regardless of what button is clicked - skipping the condition whether it is checked or not?

Yh3nG () - 10 years ago - Reply 0


you'r welcome :)

I can give you some advice help to your problem.
1) You can use {$.removeCookie('name');} for 'always check' button, or {expires: (int)} value must be '0'
2) For 'skipping button' also you can use {expires: (int)} example 1 day or 365 day, you decide :)

coskuntekin () - 10 years ago - Reply 0


First of all, thank you for this snipp. I want to show this modal only 1 time. How can I set the cookie even if the user click on the close (x) button? Thanks again.

Fernando () - 10 years ago - Reply 0


You can change "expires: 1" (js -line : 18) also you can find here (https://github.com/carhartl... how to change. Enjoy.

coskuntekin () - 10 years ago - Reply 0


This is very awesome, thanks bruh

Adam Argo () - 10 years ago - Reply 0


If I was to update content in this, how would I reset the cookie or make a new one? Just changed "my_cookie"?

Adam Argo () - 10 years ago - Reply 0


You can change "ID" ( <div class="modal fade" id="myModal">) and also you can chnage "my_cookie". If you want debug or viewing your page you can use `private window` in your browser.

Happy Coding :)

coskuntekin () - 10 years ago - Reply 0


Good look'n out bruh!

Adam Argo () - 10 years ago - Reply 0


ok see you next bootsnipp

coskuntekin () - 10 years ago - Reply 0