Example code for adding, obtaining and deleting cookies in js
The application of
cookie is very common in web pages. In this article, I will introduce how to add, get the value, and delete cookies in js. Friends who are interested should not miss it
The code is as follows:
function setCookie(name,value,time){ var oDate = new Date(); oDate.setDate(oDate.getDate()+time); document.cookie = name+"="+value+";expires="+oDate; } function getCookie(name){ var arr = document.cookie.split("; "); for(var i=0; i<arr.length; i++){ var arr2 = arr[i].split("="); if(arr2[0] == name){ return arr2[1]; } } return ""; } function removeCookie(name){ setCookie(name,"",0) }
JavaScript is a script that runs on the client, so it is generally not possible to set Session because Session runs on the server Endless. Cookies run on the client, so you can use JS to set cookies. Let's analyze the case
//已经验证过 // JavaScript Document //使用说明: //设置缓存:setCookie("name",value); //获取缓存:var name=getCookie("name"); //删除缓存:delCookie("name"); ///设置cookie function setCookie(NameOfCookie, value, expiredays) { //@参数:三个变量用来设置新的cookie: //cookie的名称,存储的Cookie值, // 以及Cookie过期的时间. // 这几行是把天数转换为合法的日期 var ExpireDate = new Date (); ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000)); // 下面这行是用来存储cookie的,只需简单的为"document.cookie"赋值即可. // 注意日期通过toGMTstring()函数被转换成了GMT时间。 document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()); } ///获取cookie值 function getCookie(NameOfCookie) { // 首先我们检查下cookie是否存在. // 如果不存在则document.cookie的长度为0 if (document.cookie.length > 0) { // 接着我们检查下cookie的名字是否存在于document.cookie // 因为不止一个cookie值存储,所以即使document.cookie的长度不为0也不能保证我们想要的名字的cookie存在 //所以我们需要这一步看看是否有我们想要的cookie //如果begin的变量值得到的是-1那么说明不存在 begin = document.cookie.indexOf(NameOfCookie+"="); if (begin != -1) { // 说明存在我们的cookie. begin += NameOfCookie.length+1;//cookie值的初始位置 end = document.cookie.indexOf(";", begin);//结束位置 if (end == -1) end = document.cookie.length;//没有;则end为字符串结束位置 return unescape(document.cookie.substring(begin, end)); } } return null; // cookie不存在返回null } ///删除cookie function delCookie (NameOfCookie) { // 该函数检查下cookie是否设置,如果设置了则将过期时间调到过去的时间; //剩下就交给操作系统适当时间清理cookie啦 if (getCookie(NameOfCookie)) { document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; } }
The above is the detailed content of Example code for adding, obtaining and deleting cookies in js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

More and more users are starting to upgrade the win11 system. Since each user has different usage habits, many users are still using the ie11 browser. So what should I do if the win11 system cannot use the ie browser? Does windows11 still support ie11? Let’s take a look at the solution. Solution to the problem that win11 cannot use the ie11 browser 1. First, right-click the start menu and select "Command Prompt (Administrator)" to open it. 2. After opening, directly enter "Netshwinsockreset" and press Enter to confirm. 3. After confirmation, enter "netshadvfirewallreset&rdqu

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Recently, many win10 users have found that their IE browser always automatically jumps to the edge browser when using computer browsers. So how to turn off the automatic jump to edge when opening IE in win10? Let this site carefully introduce to users how to automatically jump to edge and close when opening IE in win10. 1. We log in to the edge browser, click... in the upper right corner, and look for the drop-down settings option. 2. After we enter the settings, click Default Browser in the left column. 3. Finally, in the compatibility, we check the box to not allow the website to be reloaded in IE mode and restart the IE browser.

Solutions to IE shortcuts that cannot be deleted: 1. Permission issues; 2. Shortcut damage; 3. Software conflicts; 4. Registry issues; 5. Malicious software; 6. System issues; 7. Reinstall IE; 8. Use third-party tools; 9. Check the target path of the shortcut; 10. Consider other factors; 11. Consult professionals. Detailed introduction: 1. Permission issue, right-click the shortcut, select "Properties", in the "Security" tab, make sure you have sufficient permissions to delete the shortcut. If not, you can try running as an administrator, etc.
