Joke Collection Website - Bulletin headlines - How does jQuery set cookie to read, delete and empty?
How does jQuery set cookie to read, delete and empty?
Then we create a new file and import jQuery and jQuery cookie files.
Setting cookies is a short answer, just 100 USD. cookie ("name", "value"). Here I use a cookie named coco as an example.
Preview must pay attention to the need in the server environment, the local server can be established and accessed by localhost. You can see the value of the cookie in the console. Read cookie directly with $. Cookie ("name").
If you want to delete cookies, you need to set null, such as $,etc. Cookie ("coco", empty). If you want to clear the value of the cookie, just set its value to an empty string, such as $. cookie("coco "," ")。
On the page, the jQuery file is introduced first, and then the class library file of jQuery.cookie.js is introduced.
Then use the method in the script tab:
$.cookie(' the _ cookie '); Read cookie
$.cookie('the_cookie ',' the _ value '); Save cookie
$.cookie('the_cookie ',' the_value ',{ expires:7 }); Store a cookie with a 7-day expiration date.
$.cookie('the_cookie ','',{ expires:- 1 }); Delete cookie
A Cookie is a small text file stored on your machine when you browse a website. It records your user ID, password, the web pages you visited, the time you stayed, and other information. When you come to the website again, the website can read Cookie and get your relevant information, so you can take corresponding actions, such as displaying a welcome slogan on the page, or allowing you to log in directly without entering your ID and password. You can select "Settings/View Files" in the "General" tab of ie's "Tools/Interest Options" to view all Cookie stored in your computer. These files are usually named in the format of user@domain, where user is your local user name and domain is the domain name of the website you visit. If you use a NetsCape browser, it is stored in "C: \ program files \ NETSCAPE \ users". Unlike IE, Netscape uses a Cookie file to record cookies of all websites. In order to ensure the security of the Internet, we need to set Cookie appropriately. Open the Privacy tab in the Tools /Inter option (note that this setting only exists in IE6.0, and other versions of IE can be adjusted by clicking the Custom Level button in the Security tab in the Tools /Inter option). Usually, you can adjust the slider to the "medium high" or "high" position. Most forum sites need to use Cookie information. If you never go to these places, you can set the security level to "block all Cookies". If you only want to block Cookie from individual websites, you can click the Edit button to add the websites to be blocked to the list. In the Advanced button option, you can set first-party cookies and third-party cookies. The first-party Cookie is the Cookie of the website you are browsing, and the third-party Cookie is not sent to you by the website you are browsing. Usually, you should choose "Reject" for third-party Cookie, as shown in figure 1. If you need to save Cookie, you can use ie's Import and Export function to open File/Import and Export and follow the prompts. Most of the contents in Cookie are encrypted, so in our opinion, they are just meaningless alphanumeric combinations, and only the CGI processor of the server knows their true meaning. We can view more content through some software, and the Cookie information viewed by using Cookie Pal software is shown in Figure 2. It provides us with the contents, expiration, name, value and other options of the server. Among them, Server is the website that stores cookies, Expires records the time and lifetime of cookies, and the Name and value fields are specific data.
How does js set cookies to get cookies and delete cookies? JavaScript operation on cookies.
Set cookie
Function setCookie (name, value)
{
Var days = 30;
var exp = new Date();
exp . settime(exp . gettime()+Days * 24 * 60 * 60 * 1000);
document . cookie = name+" = "+escape(value)+"; expires = "+exp . togmtstring();
}
Reading cookies
Function getCookie (name)
{
Var arr, reg = new regexp ("(|)"+name+"= ([; ]*)(; |$)");
if(arr = document . cookie . match(reg))
Return UNESCAPE (arr [2]);
other
Returns null
}
Delete cookies
Function delCookie (name)
{
var exp = new Date();
exp . settime(exp . gettime()- 1);
var cval = get cookie(name);
If (cval! = empty)
document . cookie = name+" = "+cval+"; expires = "+exp . togmtstring();
}
How to clear cookies in jQuery To operate cookies in jquery, you need to use the jquery.cookie.js plug-in. It should be noted that jquery.cookie.js needs to be loaded after jquery.js
Suppose you have loaded jquery.js and jquery.cookie.js
Next, I will give some common examples to familiarize myself with the use of jquery.cookie.js
1. Create cookie:
$.cookie ('name','123'); 2. Create a cookie valid for 365 days:
$.cookie('name ',' 123 ',{ expires:365 }); Step 3 read cookie:
$.cookie ("name"); 4. Clear cookie:
$.cookie('name ',null); How does PHP read the COOKIE $_COOKIE['userCOOKIE'] set by JS? This superglobal variable is a COOKIE, where userCOOKIE is the COOKIE name, so you can change it to the cookie you want to read.
in addition
How to set COOKIE
setcookie('mycookie ','',time()+3600); The validity period is 1 hour.
Method of deleting COOKIE
setcookie('mycookie ','',time()-3600); Expired COOKIE are valid for one hour.
- Previous article:High-definition pictures of fire safety handwritten newspaper
- Next article:Zhang Bin’s personal deeds
- Related articles
- What’s up with the Chinese slogans in Google Earth?
- Why don't you sell the purple cover with gold collar and crown?
- Notice of Yangzhong Municipality on Comprehensively Promoting the Use of Zhenjiang Pass Code
- What does the head teacher say when outstanding students send banners to the school?
- Sample review meeting minutes
- Don't scribble civilized tourism slogans?
- "Intelligent second half", how does autonomous driving become a new outlet?
- Where is the Xingtai City National Security Bureau?
- Is China Datang Group New Energy Co., Ltd. a state-owned enterprise?
- Promoting the theme of handwritten newspaper materials in Putonghua