I'm building a popup with 3 buttons! Each button needs to set a cookie but with different expiry time/date. I'm using jquery.cookie
for this!
1 button is more a session cookie. So when clicking this button the popup needs to dissapear and shown again when I start a new browser screen. So NOT when I open a page in the same browser window and same website.
1 button is for "Don't show popup again" which will set cookie to 7 days
- 1 cookie is set in the AJAX succes function and is set to 365 days
I'm having trouble to get the different expiry times to set up correctly. So for example when I click the button with the session cookie then the popup shows again when I open a new page inside the website.
I can't see what I'm doing wrong! I don't get any console errors but the cookies just won't set properly.
What I have is this:
$(document).ready(function(){
var my_cookie = $.cookie('regNewsletter');
if (!my_cookie) {
setTimeout(function(){
$('#newsletter').modal('show');
}, 1000);
}
$(".close--btn").on("click", function () {
$.cookie('regNewsletter', true, {
path: '/',
domain: ''
});
});
$(".dismiss--btn").on("click", function () {
$.cookie('regNewsletter', true, {
path: '/',
domain: '',
expires: 7
});
});
console.log(my_cookie);
// code for removing cookie when session ends //
window.onbeforeunload = function() {
$.removeCookie('regNewsletter', { path: '/', domain: '' });
};
$("#newsletter .btn").click(function(e){
e.preventDefault();
$.ajax({
...
success: function(txt, status, xhr){
// some code //
$.cookie('regNewsletter', true, {
path: '/',
expires: 365
});
// etc etc //
});