0% found this document useful (0 votes)
9 views5 pages

Cookieintro

Study with javascript language

Uploaded by

janhavi1292006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Cookieintro

Study with javascript language

Uploaded by

janhavi1292006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JavaScript Cookies

Cookies are data, stored in small text files, on your computer.


When a web server has sent a web page to a browser, the connection is
shut down, and the server forgets everything about the user.
Cookies were invented to solve the problem "how to remember
information about the user":
When a user visits a web page, his/her name can be stored in a cookie.
Next time the user visits the page, the cookie "remembers" his/her name.
Cookies are saved in name-value pairs like:
username = John Doe

A cookie is an amount of information that persists between a server-side


and a client-side. A web browser stores this information at the time of
browsing.

A cookie contains the information as a string generally in the form of a


name-value pair separated by semi-colons. It maintains the state of a user
and remembers the user's information among all the web pages.

How Cookies Works?


When a user sends a request to the server, then each of that request is
treated as a new request sent by the different user.
So, to recognize the old user, we need to add the cookie with the
response from the server.
browser at the client-side.
Now, whenever a user sends a request to the server, the cookie is added
with that request automatically. Due to the cookie, the server recognizes
the users.

Cookies consist of 5 variable-length fields:


Expires − This shows the date the cookie will expire. If this is blank, the
cookie will expire when the visitor quits the browser.

Domain − The domain field provides the domain name of your site.

Path − It is the path to the directory or web page that set the cookie. This
can be left blank if you want to retrieve the cookie from any directory or
page.

Secure − If this field contains the word “secure”, then the cookie may
only be retrieved with a secure server. If this field is blank, there are no
such restrictions.

Name=Value − This depicts the cookies that are set and retrieved in the
form of key-value pairs.

Create a Cookie with JavaScript


JavaScript can create, read, and delete cookies with the document.cookie
property.
Creating cookies in JavaScript involves using the document.cookie
object to set key-value pairs and additional parameters. To create a
cookie, assign a string containing the desired cookie information to
document.cookie. This string can include attributes like expiration date,
domain, and path.
With JavaScript, a cookie can be created like this:
document.cookie = "username=ABC XYZ";
You can also add an expiry date (in UTC time). By default, the cookie is
deleted when the browser is closed:
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013
12:00:00 UTC";
With a path parameter, you can tell the browser what path the cookie
belongs to. By default, the cookie belongs to the current page.
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013
12:00:00 UTC; path=/";

Read a Cookie with JavaScript


JavaScript allows developers to read cookies using the document.cookie
property, which stores all cookies as a string. To extract specific values,
developers often create functions that parse this string. Security
considerations, like proper decoding and HttpOnly attributes, are crucial.
With JavaScript, cookies can be read like this:
let x = document.cookie;
<html>
<head>
<title>How to read a cookies</title>
<script>
document.cookie = "session=123456789";

function readAllCookies() {
let cookies = document.cookie;
console.log(cookies);
}

readAllCookies();
</script>
</head>
<body>
<h1>Read All Cookies</h1>
</body>
</html>

Change a Cookie with JavaScript


JavaScript enables the modification of cookies by updating their values
or attributes. Developers use the document.cookie property to both read
and write cookies. When changing a cookie, it’s crucial to consider
parameters like expiration date, path, and security attributes.

With JavaScript, you can change a cookie the same way as you create it:
document.cookie = "username=JJJ III; expires=Thu, 18 Dec 2013
12:00:00 UTC; path=/";

Delete a Cookie with JavaScript


JavaScript provides a way to delete cookies by setting their expiration
date in the past. When a cookie’s expiration date is in the past, the
browser automatically removes it. Developers use the document.cookie
property to delete cookies, ensuring a clean and secure user experience.

Deleting a cookie is very simple.


You don't have to specify a cookie value when you delete a cookie.
Just set the expires parameter to a past date:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00
UTC; path=/;";

You might also like