Home > Article > Web Front-end > How to determine if a cookie already exists through JavaScript
JavaScript is a widely used programming language that can be used in areas such as front-end development of websites, back-end development, mobile application development, and game development. Among them, front-end development is an important application field, and browser cookies are also an indispensable part of front-end development. In JavaScript, we can use the cookie attribute of the document object to handle browser cookies, and the way to set a cookie is to use the document.cookie attribute.
In this article, we will discuss how to determine whether a cookie already exists and set its value through JavaScript. The following are the specific steps:
The first step is to determine whether the cookie already exists
In JavaScript, we can use the indexOf() method to determine whether a specific string is in the current string exist. Therefore, we can determine whether a cookie already exists by searching the document.cookie string. The following is the sample code:
function checkCookieExist(name) { var exist = document.cookie.indexOf(name) !== -1; return exist; }
This function accepts a parameter name, which represents the name of the Cookie to be checked. This function will search the document.cookie string. If it can find the same string as name, it means that the cookie already exists and returns true, otherwise it returns false.
The second step is to set the Cookie value
If you want to set a Cookie value, we need to use the document.cookie attribute. The format of this attribute is as follows:
document.cookie = "name=value; expires=date; path=path; domain=domain; secure";
Among them, name represents the name of the cookie, and value represents the value of the cookie. expires indicates the expiration time of the cookie, path indicates the path of the cookie, domain indicates the domain name where the cookie is located, and secure indicates whether the cookie will only be transmitted under a secure connection.
The following is a sample code:
function setCookie(name, value, expires, path, domain, secure) { document.cookie = name + "=" + value + (expires ? "; expires=" + expires.toUTCString() : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "; secure" : ""); }
This function has 6 parameters, namely the name of the cookie, the value of the cookie, the expiration time of the cookie, the path of the cookie, the domain name where the cookie is located and whether This cookie is only transmitted over a secure connection. In actual use, we can choose whether to set each parameter according to the specific situation.
The third step, determine whether the cookie has expired
In the second step, we have already talked about how to set the expiration time of the cookie. If no expiration time is set, the cookie is a "session cookie" and its lifespan will be limited to one session. If an expiration time is set, the cookie will be automatically deleted after the expiration time is reached. If we want to determine whether a cookie has expired, we can use the following code:
function checkCookieExpired(name) { var cookieValue = "; " + document.cookie; var parts = cookieValue.split("; " + name + "="); if (parts.length == 2) { var expires = new Date(parts.pop().split(";").shift()); var now = new Date(); return (expires.getTime() <= now.getTime()); } else { return true; } }
This function accepts a parameter name, which represents the name of the cookie to be checked. This function first searches the document.cookie string for a string that is the same as name. If it is found, its value is taken out and converted into a Date object. Then, it will get the current time and compare the cookie's expiration time with the current time. If the expiration time is earlier than or equal to the current time, it means that the cookie has expired and returns true, otherwise it returns false.
To sum up, the above is the method to determine the setting of cookies in JavaScript. In the actual development process, we often need to set cookies to identify users, save user preferences, etc. Therefore, mastering these techniques is very useful for JavaScript developers.
The above is the detailed content of How to determine if a cookie already exists through JavaScript. For more information, please follow other related articles on the PHP Chinese website!