jQuery Cookie plugin
jQuery can manipulate cookies through the jquery.cookie.js plugin.
Official site:http://plugins.jquery.com/cookie/
Github site:https://github.com/carhartl/jquery-cookie
Before using jquery.cookie.js you need to include jQuery first:
<script src="/path/to/jquery.min.js" script>
<script src="/path/to/jquery.cookie.js" script>We can use third-party resources to include both files:
<script src="https://cdn.staticfile.org/jquery/3.4.0/jquery.min.js" script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js" script>Create a cookie:
$.cookie('name', 'value');
If no expiry is specified, the cookie expires when the browser is closed.
Create a cookie that expires after 7 days:
$.cookie('name', 'value', { expires: 7 });
Create a cookie with a valid path set to the website root:
$.cookie('name', 'value', { expires: 7, path: '/' });
**Note:** By default, only the webpage that set a cookie can read it. If you want one page to read a cookie set by another page, you must set the cookie's path. The cookie path sets the top-level directory that can read the cookie. Setting this path to the website root allows all pages to read each other's cookies (generally you should not set it this way, to avoid conflicts).
Read a cookie:
$.cookie('name'); // => "value"
$.cookie('nothing'); // => undefined
Read all cookie information:
$.cookie(); // => { "name": "value" }
Delete a cookie:
//
Returns true if the cookie was deleted successfully, otherwise false
$.
removeCookie
(
'
name
'
)
;
//
=> true
$.
removeCookie
(
'
nothing
'
)
;
//
=> false
//
When writing with a path, reading must use the same attributes (path, domain)
$.
cookie
(
'
name
'
,
'
value
'
,
{
path
:
'
/
'
}
)
;
//
The following code [deletion failed]
$.
removeCookie
(
'
name
'
)
;
//
=> false
//
The following code [deletion succeeded]
$.
removeCookie
(
'
name
'
,
{
path
:
'
/
'
}
)
;
//
=> true
**Note:** When deleting a cookie, you must pass exactly the same path, domain and secure options used to set the cookie.
Example
$
(
document
)
.
ready
(
function
(
)
{
$.
cookie
(
'
name
'
,
'
runoob
'
)
;
//
Create a cookie
name
= $.
cookie
(
'
name
'
)
;
//
Read a cookie
$
(
"
#test
"
)
.
text
(
name
)
; $.
cookie
(
'
name2
'
,
'
runoob2
'
,
{
expires
:
7
,
path
:
'
/
'
}
)
;
name2
= $.
cookie
(
'
name2
'
)
; $
(
"
#test2
"
)
.
text
(
name2
)
;
}
)
;
Try it »
After running, we can view the cookie information in the browser, as shown below:

Parameter Description
raw
Default value: false.
By default, cookies are automatically encoded and decoded when read and written (encoded with encodeURIComponent, decoded with decodeURIComponent). To turn this off, set raw:true:
$.cookie.raw = true;
json
To store and read cookie data using json, you do not need to use JSON.stringify and JSON.parse.
$.cookie.json = true;
expires
expires: 365
Defines the cookie's expiry time; the value can be a number (in days from the time the cookie is created) or a Date object. If omitted, the created cookie is a session cookie and will be deleted when the user exits the browser.
path
path: '/'
By default, only the webpage that set the cookie can read it.
Defines the cookie's valid path. By default, this parameter's value is the path of the webpage that created the cookie (standard browser behavior).
If you want to access this cookie across the whole website, set the valid path this way: path: '/'. If you want to delete a cookie with a defined valid path, you need to include that path when calling the function:
$.cookie('the_cookie', null,{ path: '/' });
domain
domain: 'example.com'
Default value: the domain of the webpage that created the cookie.
secure
secure: true
Default value: false. If true, the cookie must be transmitted using a secure protocol (HTTPS).