Cookies are often mentioned by everyone now, so what are cookies and what are their functions? Cookies are data packets that allow web pages to have a memory function and store certain information on a certain computer. The working principle of cookies is that they are first written to the client's system by the server. Every time you visit this webpage in the future, the client will first send cookies to the server, and then the server will make a judgment, and then generate HTML code and return it to the client. Through this principle, the server can generate different cookie files according to different users, so that when the user visits the same site again, different page information can be returned based on different cookie files.
In layman’s terms: Cookies are a technology that allows the website server to store a small amount of data on the client’s hard drive or memory, or to read data from the client’s hard drive. Cookies are a very small text file placed on your hard drive by the web server when you browse a website. It can record information such as your user ID, password, web pages you have browsed, and the time you stayed on it. When you come to the website again, the website learns your relevant information by reading the cookies, and can take corresponding actions, such as displaying a slogan welcoming you on the page, or allowing you to log in directly without entering your ID or password. etc. Cookie files silently accompany the browser into our local hard drive. When we browse a site, the site is likely to upload the cookie file that records our privacy to the local hard drive.
Cookies are similar to localStorage. If you want to delete it, you can only delete it manually. Of course, in addition to deleting Cookies, you can also set, retrieve, add, delete, modify, and query the database. To get and set Cookies, in addition to using the cookie plug-in, we can also operate Cookies through js. Here, I would like to introduce to you how to obtain, set, and delete Cookies through js.
(1) Get Cookies:
getCookie(name){ var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return unescape(arr[2]); else return null; }
(2) Set Cookies:
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(); }
(3 )Delete Cookies:
delCookie(name){ var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=this.getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString(); }
Example:
setCookie("id","1");getCookie("id");delCookie("id");
Cookies are often mentioned by everyone now, so what are Cookies and what are their functions? Cookies are data packets that allow web pages to have a memory function and store certain information on a certain computer. The working principle of cookies is that they are first written to the client's system by the server. Every time you visit this webpage in the future, the client will first send cookies to the server, and then the server will make a judgment, and then generate HTML code and return it to the client. Through this principle, the server can generate different cookie files according to different users, so that when the user visits the same site again, different page information can be returned based on different cookie files.
In layman’s terms: Cookies are a technology that allows the website server to store a small amount of data on the client’s hard drive or memory, or to read data from the client’s hard drive. Cookies are a very small text file placed on your hard drive by the web server when you browse a website. It can record information such as your user ID, password, web pages you have browsed, and the time you stayed on it. When you come to the website again, the website learns your relevant information by reading the cookies, and can take corresponding actions, such as displaying a slogan welcoming you on the page, or allowing you to log in directly without entering your ID or password. etc. Cookie files silently accompany the browser into our local hard drive. When we browse a certain site, the site is likely to upload the cookie file that records our privacy to the local hard drive.
Cookies are similar to localStorage. If you want to delete it, you can only delete it manually. Of course, in addition to deleting Cookies, you can also set, obtain, add, delete, modify, and query the database. To get and set Cookies, in addition to using the cookie plug-in, we can also operate Cookies through js. Here, I would like to introduce to you how to obtain, set, and delete Cookies through js.
(1) Get Cookies:
getCookie(name){ var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return unescape(arr[2]); else return null; }
(2) Set Cookies:
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(); }
(3 )Delete Cookies:
delCookie(name){ var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=this.getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString(); }
Example:
setCookie("id","1");getCookie("id");delCookie("id");
Related recommendations:
Share the example code of JS using cookies to set a pop-up box every 24 hours
The above is the detailed content of js browser cookies setting example. For more information, please follow other related articles on the PHP Chinese website!