Сookies in JavaScript. Setting, getting. Code example open

Сookies in JavaScript. Setting, getting. Code example

To work with Cookies in the Document Object, the Cookie property is intended.

Set cookies:

    document.cookie = "login=best-user;";

In this case, a cookie called “login” is set, which has the value “best-user“.

The cookie string accepts up to six different parameters:

  • Cookie name (set in the example above)
  • Cookie value (set in the example above)
  • Expiry date (expires)
  • The path where the cookie is valid (path)
  • Domain where cookie is valid (domain)
  • Security setting (secure)

For example, set the cookie expiration date:

    document.cookie = "login=best-user;expires=Mon, 31 Aug 2022 00:00:00 GMT;";//The login cookie will expire on Monday 31 August 2022 at 00:00.

We can generate this parameter programmatically. To do this, we can use the toUTCString() method of the Date object:

    var expire = new Date();
    expire.setHours(expire.getHours() + 4);//add 4 hours from current time
    document.cookie = "login=best-user;expires=" + expire.toUTCString() + ";";   

If in a friend we need to set cookies for a specific path on the site, then we can use the path parameter.
For example, we want to set cookies only for the path www.mysite.com/shop:

    document.cookie = "login=best-user;expires=Mon, 31 Aug 2022 00:00:00 GMT;path=/shop;";

For other paths on the site, such as www.mysite.com/users, these cookies will not be available.

If our site has multiple domains and we want to set cookies directly for a specific domain, then the domain parameter can be used:

    document.cookie = "login=best-user;expires=Mon, 31 Aug 2022 00:00:00 GMT;path=/;domain=blog.mysite.com;";

The path=/ parameter specifies that cookies will be available for all directories and paths of the blog.mysite.com subdomain.

The last parameter – secure specifies the use of SSL and is suitable for sites using the https protocol.
If this parameter is set to true, then cookies will only be used when establishing a secure SSL connection.

!By default, this parameter is false.

    document.cookie = "login=best-user;expires=Mon, 31 Aug 2022 00:00:00 GMT;path=/;domain=blog.mysite.com;secure=true";

Getting cookies

For the simplest extraction of cookies from the browser, it is enough to refer to the document.cookie property:

    var expire = new Date();
    expire.setHours(expire.getHours() + 4);

    document.cookie = "city=Berlin;expires=" + expire.toUTCString() + ";";
    document.cookie = "country=Germany;expires=" + expire.toUTCString() + ";";
    document.cookie = "login=tom32;";

    document.write(document.cookie);

The retrieved cookies do not include the expires, path, domain, and secure parameters. In addition, the cookies themselves are separated by a dot with comma, so you still need to do some transformations to get their name and value:

    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {

        var parts = cookies[i].split("="),
        name = parts[0],
        value = parts[1];
        document.write("Cookie: " + name);
        document.write("Value: " + value);
    }
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation