Working of cookies in JavaScript

Working of cookies in JavaScript

newCookie is a string of the type key=value in the code above. This technique only allows you to set/update a single cookie at a time. Consider also that:

  • Any of the following cookie attribute values, preceded by a semi-colon separator, can optionally follow the key-value combination, defining the Cookie to set/update:
    • ;domain=domain ( or, for example). If not provided, this defaults to the host part of the current document location. Contrary to previous standards, leading dots in domain names are ignored, although browsers may refuse to set the Cookie containing such dots. Subdomains are always included if a domain is provided.
    • Note: Note that the domain must match the JavaScript origin domain. Using foreign domains to set cookies will be quietly ignored.
    • ;max-age=max-age-in-seconds (e.g., for a year, 60*60*24*365 or 31536000)
    • ;expires=date-in-GMTString-format If neither expires nor max-age are provided, the session will end.

Javascript Set Cookie

Documents can be used to make cookies. This is a cookie property.

document. Cookie = "cookiename=cookievalue" You may also include an expiration date for your Cookie, which will ensure that it is deleted from the computer on the stated date. The expiration date should be formatted in UTC/GMT. If the expiration date is not specified, the Cookie will be deleted when the user exits the browser.

"cookiename=cookievalue; expires= Thu, 21 Aug 2014 20:00:00 UTC"
document.cookie =

You may optionally give the domain and path to define which domain the Cookie belongs to, as well as the directories inside that domain. A cookie belongs to the page that sets it by default.

JavaScript get Cookie

You can get to the Cookie using this method, which will show you all of the cookies saved for the current website.

document.cookie = var x