JavaScript - Cookie Attributes
JavaScript - Cookie Attributes
Cookie Attributes
The JavaScript cookie attributes are used to set additional information about a cookie such as path, domain, expiry date, etc. In JavaScript, you can specify the cookie attributes while setting up a new cookie or updating the cookie. For example, you can set the cookieβs expiry date using the βexpiresβ attributes.
In simple terms, cookie attributes are used to control the behavior of the cookies and how the cookie is used in the browser.
Here, we have listed all cookie attributes in the table below with its description.
AttributeDescriptionDefault ValueName/ValueIt is used to store the cookies in the browser.DomainTo specify the domain for whose cookie is valid.Website of domain. For example, codescompiler.comPathThe path to the directory or web page that sets the cookie./ (entire domain)ExpiresIt is used to specify the date and time when the cookie should expire.Current sessionMax-AgeIt is used to specify the time limit after that cookie should expire.Current sessionSecureIf this field contains the word βsecureβ, then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists.falseHttpOnlyIt prevents accessing the cookies via JavaScript to make cookies secure.falseSameSiteIt is used to specify how third-party cookies should be handled.LaxPriorityIt defines the priority of the cookies.1000Site/ServiceTo get information about the origin site of the cookie.SourcePortTo get the port of the source for the cookie.StoragePartitionIt defines which storage partition is used to store cookies.SizeIt represents the size of the cookies.The size depends on the text length.
However, above all, attributes are optional.
Also, you canβt manipulate all attributes of the cookies. The browser sets some attributes.
Check the Attribute Value in the Browser
You can set the attributes to the cookie, but you canβt access the attributes. To check whether the attribute is set, you can use the browser console.
Follow the steps below to check cookies in the browser console.
Step 1β Right click in the browser. It will open the menu. You need to select the βinspectβ option. It will open the developer tool.
Step 2β After that, you need to go to the Application/ Storage tab.
Step 3β In the sidebar, select βcookies.
Step 4β Now, click on any cookies to check their name, value, and other attribute values.
The above steps are only for the Chrome web browser. The step can differ according to what browser you are using.
Here, you will learn each attribute one by one with examples.
Cookieβs Name/Value Attribute
The Name attribute is used to store the cookie data. It takes the data as a value. If you want to use the special characters in the value of the βNameβ attribute, you need to encode the text using the encodeURIComponent() method.
Syntax
Follow the syntax below to set the Name attribute of the cookie.
let value = encodeURIComponent(cookieValue);
document.cookie = "name=" + value + ";";In the above syntax, we have encoded the βcookieValueβ using the encodeURIComponent() method and used the encoded value as a name attribute value.
Example
In the below code, we set the βsubscribedβ cookie with a βfalseβ value. You can click the read cookies button to get the cookies.
Cookieβs Path Attribute
The Path attribute is used to set the scope of the cookie. It defines where cookies should be accessible on the website. You may set the relative or absolute path as a Path attribute value.
If you set the relative path, all the cookies can be accessible everywhere in the particular or sub-directory.
Syntax
Follow the syntax below to set the Path attribute in the cookie.
document.cookie = "name=value;path=pathStr";In the above syntax, you need to replace the βpathStrβ with the actual path string.
Example
In the below code, we set the path for the cookie. Here, we set the / (home route). So, cookies can be accessible on each webpage of the website. You may try to get the cookie on the different web pages of the website.
Cookie Expires Attribute
The βexpiresβ attribute is used to set the expiry date for the cookie. It takes the date string as a value.
If you set 0 or past date as a value of the βexpires, the browser will automatically delete the cookie.
Syntax
Follow the syntax below to set the expires attribute in the cookie.
document.cookie = "name=value;expires=dateStr";In the above syntax, you need to replace the βdateStrβ with the date string.
Example
In the code below, we set the product cookie. Also, we set the expiry date in 2050.
You may try to set the past expiry date and try to access the cookie. You wonβt be able to find the cookie.
Cookieβs maxAge Attribute
The βmaxAgeβ attribute is an alternative to the βexpiresβ attribute. It is used to specify the lifetime of the cookie. It takes the seconds as a value.
When the lifetime of the cookie is finished, it will automatically get deleted.
Syntax
Follow the syntax below to pass the βmaxAgeβ attribute to the cookie.
document.cookie = "name=value;max-ge=age;";In the above syntax, you need to replace the βageβ with the number of seconds.
Example
In the below code, we set the total number of seconds equal to 10 days as a value of the maxAge attribute. You can set the lifetime of 1 second for the cookie and try to access the cookie after 1 second.
Cookie Domain Attribute
The domain attribute is used to specify the domain for which the cookie is valid. The default value of the domain from which you are making a request. You may set the domain attribute to set the subdomains.
Syntax
Follow the syntax below to set the value of the domain attribute in the cookie.
document.cookie = "name=value;domain:domain_name ";In the above syntax, replace the βdomain_nameβ with the actual domain, like example.com.
Example
In the below code, we set the βcodescompiler.comβ domain for the cookie.
Similarly, you can also update the attribute values. For example, you can extend the expiry time of the cookie.