How can we add a persistent cookie to js-cookie?
Asked Answered
R

1

2

Context : I have set a cookie into my Rails app, to display to users only one time a popup (bootstrap modal).

Goal : I want to set the cookie for 1 month, with no

Issue : the code bellow works fine but when you close the navigator, the cookie is restarted. I think is the difference between session vs persistent cookie.

The stack of the app :

The code :

 $(document).ready(function() {
     if (Cookies('pop') == null) {
         $('#MyModal').modal('show');
         Cookies('pop', '31');
     }
 });

JS-cookie code here : https://github.com/js-cookie/js-cookie/blob/latest/src/js.cookie.js

Thanks!

Robedechambre answered 7/1, 2019 at 14:36 Comment(0)
T
1

According to the docs, this should work:

Cookies.set('pop', 'any truthy value', { expires: 31 });

Example:

$(document).ready(function() {
    if (Cookies('pop') == null) {
        $('#MyModal').modal('show');
        Cookies('pop', '31', { expires: 31 });
        // or:
        // Cookies('pop', 'shown', { expires: 31 });
        // Cookies('pop', true, { expires: 31 });
        // Cookies('pop', 'etc.', { expires: 31 });
    }
});
Tisza answered 7/1, 2019 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.