Plugin for Cookies in JavaScript

- Andrés Cruz

En español

Plugin for Cookies in JavaScript

Several previous entries ago we talked about the use of Cookies using native JavaScript:

First steps with Cookies in JavaScript

Which is a very useful scheme for small sites that do not use many libraries and/or that we want to make few saves in cookies; The problem with working with Cookies natively is that it can be complex or become a real mess when we start updating variables, handling many variables or multiple queries to Cookies, and this is precisely because of the way they are stored, which is purely a String separated by semicolons (,).

To avoid the previous problem we can use a plugin that allows us to easily work with Cookies in JavaScript.

As you can see in the source code, the plugin is really small which means the loading impact for our website is minimal.

First steps with JavaScript Cookie

Una vez que incluyamos el JavaScript anterior, su uso es realmente fácil; basta con emplear la variable Cookies de la siguiente manera para guardar un valor dentro de una Cookie:

Cookies.set('name', 'value');

Para crear una Cookie con el valorvalue, y como todo método set() tiene su get(), para obtener el valor anterior tenemos:

Cookies.get('name'); // => 'value'

Establecer duración de la cookie:

Si queremos establecer un tiempo de vida de la cookie; por ejemplo 7 días:

Cookies.set('name', 'value', { expires: 7 });

Eliminar una cookie:

Por último, para remover una Cookie:

Cookies.remove('name');

Estos son los métodos principales, de igual manera puedes consultar la documentación oficial en el enlace a github al principio de esta entrada.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 2d 02:02!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!