SoFunction
Updated on 2025-03-02

Sample code for jQuery operation cookies

Cookies

Definition: A technology for letting the website server store a small amount of data on the client's hard disk or memory and read data from the client's hard disk;

Download and import: based on jquery; first introduce jquery, then introduce:; download:/cookie/

<script type="text/javascript" src="js/"></script>
<script type="text/javascript" src="js/"></script> 

1. Add a "session cookie"

$.cookie('the_cookie', 'the_value'); 

The cookie validity time is not specified here. The cookie created is valid until the user closes the browser, so it is called a "session cookie".

2. Create a cookie and set the valid time to 7 days

$.cookie('the_cookie', 'the_value', { expires: 7 }); 

This indicates the time of validity of the cookie, and the cookie created is called "persistent cookie". Note that the unit is: day;

3. Create a cookie and set a valid path to the cookie

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' }); 

By default, only the cookie is read by web pages that set the cookie. If you want one page to read the cookie set by another page, you must set the path to the cookie. The path to the cookie is used to set a top-level directory that can read the cookie. Setting this path to the root directory of the website allows all web pages to read cookies to each other (usually don't set this way to prevent conflicts).

4. Read cookies

$.cookie('the_cookie'); 

5. Delete cookies

$.cookie('the_cookie', null);  //By passingnullAscookieThe value is 

6. Optional parameters

$.cookie('the_cookie','the_value',{

  expires:7, 

  path:'/',

  domain:'',

  secure:true

})  

7. Parameters

expires: (Number|Date) validity period; when setting an integer, the unit is days; a date object can also be set as the expiration date of the cookie;

path: (String) The page path to create the cookie;

domain: (String) creates the page domain name of the cookie;

secure: (Booblean) If set to true, the transmission of this cookie will require a security protocol, such as: HTTPS;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.