I. jquery Operation cookie
Jquery. cookie. js configuration
First, it contains the library file of jQuery, followed by the library file of jquery. cookie. js.
<Script type = "text/javascript" src = "js/jquery-1.6.2.min.js"> </script>
<Script type = "text/javascript" src = "js/jquery. cookie. js"> </script>
Usage
1. Add a new session cookie:
$. Cookie (the _ cookie, the _ value ');
Note: When the cookie validity period is not specified, the created cookie is valid until the user closes the browser by default.
"Session cookie )".
2. Create a cookie and set the validity period to 7 days:
$. Cookie (the _ cookie, the _ value, {expires: 7 });
Note: When the cookie validity period is specified, the created cookie is called "persistent cookie )".
3. Create a cookie and set the valid cookie path:
$. Cookie (the _ cookie, the _ value, {expires: 7, path :'/'});
Note: By default, only webpages with cookie settings can read the cookie. If you want to read one page from another page
The path of the cookie. The cookie path is used to set the top-level directory that can read cookies. Set this
Path is set as the root directory of the website, so that all webpages can read cookies from each other (generally do not set this way to prevent conflicts ).
4. Read cookie:
$. Cookie (the _ cookie); // cookie existence => 'The _ value'
$. Cookie ('not _ existing'); // cookie does not exist => null
5. Delete the cookie and pass null as the cookie value:
$. Cookie (the _ cookie, null );
II. JS cookie setting method
JavaScript is a script running on the client. Therefore, the Session cannot be set because the Session is running on the server.
The cookie runs on the client, so you can use JS to set the cookie.
Therefore, the best solution to this problem is to use cookies to save the value of this variable. How can we set and read cookies?
| The code is as follows: |
Copy code |
<Script> // Set cookie Function setCookie (cname, cvalue, exdays ){ Var d = new Date (); D. setTime (d. getTime () + (exdays * 24x60*60*1000 )); Var expires = "expires =" + d. toUTCString (); Document. cookie = cname + "=" + cvalue + ";" + expires; } // Obtain the cookie Function getCookie (cname ){ Var name = cname + "= "; Var ca = document. cookie. split (';'); For (var I = 0; I <ca. length; I ++ ){ Var c = ca [I]; While (c. charAt (0) = '') c = c. substring (1 ); If (c. indexOf (name )! =-1) return c. substring (name. length, c. length ); } Return ""; } // Clear cookie Function clearCookie (name ){ SetCookie (name, "",-1 ); } Function checkCookie (){ Var user = getCookie ("username "); If (user! = ""){ Alert ("Welcome again" + user ); } Else { User = prompt ("Please enter your name :",""); If (user! = "" & User! = Null ){ SetCookie ("username", user, 365 ); } } } CheckCookie (); </Script> </Ca> </script> |
Many friends found that,
When using JS to store cookies, it is found that the same js method is called to store cookies in different directories, and the values that cannot be obtained or retrieved from other directories are incorrect, this is because no storage path is added to the cookie, and the access value is incorrect.
Next, let's improve the above program:
| The code is as follows: |
Copy code |
// Set cookie Function setCookie (cname, cvalue, exdays ){ Var d = new Date (); D. setTime (d. getTime () + (exdays * 24x60*60*1000 )); Var expires = "expires =" + d. toUTCString (); Document. cookie = cname + "=" + cvalue + ";" + expires + "; path = /"; }
|
Add a path after the parameter.