📜 ⬆️ ⬇️

work with cookies from javascript

Hello!

Today I want to share with those who are not in the subject, a theory about how to work with a cookie from JS

JS does not provide a convenient API for working with cookies. And this is not bad in principle, it could be worse (for example, js would not implement working with cookies at all), but still, it’s better when you can read cookies with one instruction (which is not possible with native js yet).
')
There are many frameworks and plugins to them that make up for this flaw. However, there are projects where it is impractical to connect the framework only for the convenience of working with cookies.

Actually, I have set and implemented the task of creating methods for managing cookies, with beautiful accessors of the type:

cookie.set('bla', 'blabla');
cookie.get('bla');


code.google.com/p/jscookie - google code page

A bit of theory about the mechanism provided by js for cookie management:

All that we have is the document.cookie property, it does not implement any methods familiar to the ordinary programmer, such as document.cookie.get ('bla');

Read cookie


document.cookie contains a set of values ​​cookie_name = cookie_value; (separated by ";" (semicolon plus space)), it follows that to get the value of a specific cookie - you need to parse the entire line and pull out the necessary data. Remarkably, the property contains the name = value pairs, does not contain any additional data about cookies such as: expires time, path, domain, secure.

Create / update cookie


document.cookie behaves like a string, but not normal. imagine that we have 2 cookies, key1 = val1 and key2 = val2.

alert(document.cookie) //key1=val1; key2=val2;

In order to add a new key3 key3 = val3

document.cookie= 'key3=val3; ';
alert(document.cookie) //key1=val1; key2=val2; key3=val3;

In order to update the cookie, set the key2 value hello world

document.cookie= 'key2=hello world; ';
alert(document.cookie) //key1=val1; key2=hello world; key3=val3;

Delete cookie


Now I’ll tell you a little about the cookies as such, I don’t claim to have complete and thorough knowledge of this subject, I’ll just tell you a little bit what I know.

In addition to the name and meaning, Cook has several important properties in its arsenal, namely:

expires - the time after which the cookie is deleted.
domain - the domain for which the cookie is valid, roughly speaking, if the cookie was created on the js.com domain, then on no other domain will it be visible.
path - a set of documents, when requested by which the browser will send a cookie
secure - property-flag allowing to send a cookie to browser only for https connection

In general, all this is written in the form document.cookie = 'key4 = val4; [expires = Sat, 09 Jan 2010 16:14:53 GMT; ] [path = /; ] [domain = js.com; ] [secure; ]

The trick with deleting cookies is to update the cookie with the expires property pointing to the past, for example:

document.cookie= 'key2=; expires=Mon, 05 Jul 1982 16:37:55 GMT; ';
alert(document.cookie)//key1=val1; key3=val3;

Actually everything

UPD: this is my first post, please do not kick much. I hope someone will be useful

Source: https://habr.com/ru/post/74217/


All Articles