JavaScript Cookie
Cookies are used to store user information on web pages.
What is a cookie?
A cookie is some data stored in a text file on your computer.
When a web server sends a web page to the browser, after the connection is closed, the server does not remember the user's information.
The purpose of cookies is to solve "how to remember the user's information on the client side":
- When a user visits a web page, their name can be recorded in a cookie.
- When the user visits the page next time, the visit record can be read from the cookie.
Cookies are stored as name/value pairs, as shown below:
username=John Doe
When the browser requests a web page from the server, cookies belonging to that page are added to the request. In this way, the server obtains the user's information.
Creating cookies with JavaScript
JavaScript can use the document.cookie property to create, read, and delete cookies.
In JavaScript, creating a cookie is shown below:
document.cookie="username=John Doe";
You can also add an expiration time (in UTC or GMT) to a cookie. By default, a cookie is deleted when the browser closes:
document.cookie="username=John Doe; expires=Thu, 18 Dec 2043 12:00:00 GMT";
You can use the path parameter to tell the browser the cookie's path. By default, a cookie belongs to the current page.
document.cookie="username=John Doe; expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";
Reading cookies with JavaScript
In JavaScript, you can use the following code to read a cookie:
var x = document.cookie;
![]() |
document.cookie returns all cookies as a string, in the format: cookie1=value; cookie2=value; cookie3=value; |
|---|
Modifying cookies with JavaScript
In JavaScript, modifying a cookie is similar to creating one, as shown below:
document.cookie="username=John Smith; expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";
The old cookie will be overwritten.
Deleting cookies with JavaScript
Deleting a cookie is very simple. You only need to set the expires parameter to a past time, for example, set to Thu, 01 Jan 1970 00:00:00 GMT:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
Note that you do not need to specify the cookie's value when deleting it.
Cookie strings
The document.cookie property looks like an ordinary text string, but it is not.
Even if you write a complete cookie string into document.cookie, when you read the cookie information again, it is displayed as name/value pairs.
If you set a new cookie, the old cookie is not overwritten. The new cookie is added to document.cookie, so if you read document.cookie again, you will get data like the following:
cookie1=value; cookie2=value;
If you need to find a specific cookie value, you must create a JavaScript function to search for the cookie value in the cookie string.
JavaScript cookie example
In the following example, we will create a cookie to store the visitor's name.
First, when a visitor visits the web page, they will be asked to enter their name. The name will be stored in a cookie.
When the visitor next visits the page, they will see a welcome message.
In this example we will create 3 JavaScript functions:
- A function to set the cookie value
- A function to get the cookie value
- A function to check the cookie value
A function to set the cookie value
First, we create a function to store the visitor's name:
function
setCookie
(
cname
,
cvalue
,
exdays
)
{
var
d
=
new
Date
(
)
;
d
.
setTime
(
d
.
getTime
(
)
(
exdays
24
60
60
1000
)
)
;
var
expires
=
"
expires=
"
d
.
toGMTString
(
)
;
document
.
cookie
=
cname
"
=
"
cvalue
"
;
"
expires
;
}
Function explanation:
In the above function parameters, the cookie name is cname, the cookie value is cvalue, and the cookie expiration time is set by expires.
This function sets the cookie name, cookie value, and cookie expiration time.
A function to get the cookie value
Then, we create a function to return the value of a specified cookie:
function
getCookie
(
cname
)
{
var
name
=
cname
"
=
"
;
var
ca
=
document
.
cookie
.
split
(
'
;
'
)
;
for
(
var
i
=
0
;
i
<
ca
.
length
;
i
++
)
{
var
c
=
ca
[
i
]
.
trim
(
)
;
if
(
c
.
indexOf
(
name
)
==
0
)
return
c
.
substring
(
name
.
length
,
c
.
length
)
;
}
return
"
"
;
}
Function explanation:
The parameter for the cookie name is cname.
Create a text variable to retrieve the specified cookie: cname + "=".
Use a semicolon to split the document.cookie string and assign the resulting array of strings to ca (ca = document.cookie.split(';')).
Loop through the ca array (i=0;i If the cookie is found (c.indexOf(name) == 0), return the cookie's value (c.substring(name.length,c.length). If the cookie is not found, return "". Finally, we can create a function to check whether the cookie has been created. If the cookie is set, a greeting message will be displayed. If the cookie is not set, a popup will be displayed asking for the visitor's name, and the setCookie function will be called to store the visitor's name for 365 days: function checkCookie ( ) { var username = getCookie ( " username " ) ; if ( username != " " ) { alert ( " Welcome again " username ) ; } else { username = prompt ( " Please enter your name: " , " " ) ; if ( username != " " && username != null ) { setCookie ( " username " , username , 365 ) ; } } } function setCookie ( cname , cvalue , exdays ) { var d = new Date ( ) ; d . setTime ( d . getTime ( ) ( exdays 24 60 60 1000 ) ) ; var expires = " expires= " d . toGMTString ( ) ; document . cookie = cname " = " cvalue " ; " expires ; } function getCookie ( cname ) { var name = cname " = " ; var ca = document . cookie . split ( ' ; ' ) ; for ( var i = 0 ; i < ca . length ; i ++ ) { var c = ca [ i ] . trim ( ) ; if ( c . indexOf ( name ) == 0 ) { return c . substring ( name . length , c . length ) ; } } return " " ; } function checkCookie ( ) { var user = getCookie ( " username " ) ; if ( user != " " ) { alert ( " Welcome " user " Visit again " ) ; } else { user = prompt ( " Please enter your name: " , " " ) ; if ( user != " " && user != null ) { setCookie ( " username " , user , 30 ) ; } } } Try it » The following example executes the checkCookie() function when the page loads.
A function to check the cookie value
Complete example
Example
